当前位置: 技术问答>java相关
Swing中的JTable这个问题请高手解答,谢谢!!………
来源: 互联网 发布时间:2015-02-12
本文导语: 照搬一程序如下: import javax.swing.*; import javax.swing.table.*; public class JTableTest extends JFrame{ public static void main(String args[]) { JTableTest test=new JTableTest(); test.setSize(300,200); test.show(); } public JTableTest() { ...
照搬一程序如下:
import javax.swing.*;
import javax.swing.table.*;
public class JTableTest extends JFrame{
public static void main(String args[]) {
JTableTest test=new JTableTest();
test.setSize(300,200);
test.show();
}
public JTableTest() {
JTable jt=new JTable(new MyTableModel());
jt.setRowHeight(20);
getContentPane().add(new JScrollPane(jt));
}
}
class MyTableModel extends AbstractTableModel {
private String[] columns={"姓名","年龄","已婚"};
private Object[][] data={{"王小明",new Integer(28),new Boolean(true)},
{"陈小华",new Integer(26),new Boolean(false)},
{"张小英",new Integer(35),new Boolean(true)},
{"林小莉",new Integer(18),new Boolean(false)}};
public int getColumnCount() {
return (columns.length);
}
public int getRowCount() {
return(data.length);
}
public String getColumnName(int col) {
return(columns[col]);
}
public Object getValueAt(int row,int col) {
return(data[row][col]);
}
public Class getColumnClass(int col) {
return(getValueAt(0,col).getClass());
}
public boolean isCellEditable(int row, int col) {
return(col>0);
}
public void setValueAt(Object value, int row, int col) {
if(col==1) {
try{
data[row][col]=new Integer((String)value);
}
catch(NumberFormatException e) {
}
}
else
data[row][col]=value;
fireTableCellUpdated(row,col);
}
}
再javac JTableTest.java 成功
java JTableTest 也成功,并出现图框,但点选其中任意框后,在点选其它框则出现异常,提示如下:
Exception occurred during event dispatching:
java.lang.ClassCastException: java.lang.Integer
at MyTableModel.setValueAt(JTableTest.java:42)
at javax.swing.JTable.setValueAt(JTable.java:1734)
at javax.swing.JTable.editingStopped(JTable.java:2989)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.
java:112)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultC
ellEditor.java:242)
at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:
176)
at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:3308)
at javax.swing.JTable.editCellAt(JTable.java:2359)
at javax.swing.plaf.basic.BasicTableUI$MouseInputHandler.mousePressed(Ba
sicTableUI.java:464)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:22
1)
at java.awt.Component.processMouseEvent(Component.java:3714)
at java.awt.Component.processEvent(Component.java:3546)
at java.awt.Container.processEvent(Container.java:1167)
at java.awt.Component.dispatchEventImpl(Component.java:2595)
at java.awt.Container.dispatchEventImpl(Container.java:1216)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2458
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2217)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2132)
at java.awt.Container.dispatchEventImpl(Container.java:1203)
at java.awt.Window.dispatchEventImpl(Window.java:918)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:336)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:134)
我把public Class getColumnClass(int col) 除掉后,则修改图框内容不会发生异常,但记录的最后一项“boolean"会显示为字符串,而不是图框形式。
请高手释疑!!!!谢谢!!!
import javax.swing.*;
import javax.swing.table.*;
public class JTableTest extends JFrame{
public static void main(String args[]) {
JTableTest test=new JTableTest();
test.setSize(300,200);
test.show();
}
public JTableTest() {
JTable jt=new JTable(new MyTableModel());
jt.setRowHeight(20);
getContentPane().add(new JScrollPane(jt));
}
}
class MyTableModel extends AbstractTableModel {
private String[] columns={"姓名","年龄","已婚"};
private Object[][] data={{"王小明",new Integer(28),new Boolean(true)},
{"陈小华",new Integer(26),new Boolean(false)},
{"张小英",new Integer(35),new Boolean(true)},
{"林小莉",new Integer(18),new Boolean(false)}};
public int getColumnCount() {
return (columns.length);
}
public int getRowCount() {
return(data.length);
}
public String getColumnName(int col) {
return(columns[col]);
}
public Object getValueAt(int row,int col) {
return(data[row][col]);
}
public Class getColumnClass(int col) {
return(getValueAt(0,col).getClass());
}
public boolean isCellEditable(int row, int col) {
return(col>0);
}
public void setValueAt(Object value, int row, int col) {
if(col==1) {
try{
data[row][col]=new Integer((String)value);
}
catch(NumberFormatException e) {
}
}
else
data[row][col]=value;
fireTableCellUpdated(row,col);
}
}
再javac JTableTest.java 成功
java JTableTest 也成功,并出现图框,但点选其中任意框后,在点选其它框则出现异常,提示如下:
Exception occurred during event dispatching:
java.lang.ClassCastException: java.lang.Integer
at MyTableModel.setValueAt(JTableTest.java:42)
at javax.swing.JTable.setValueAt(JTable.java:1734)
at javax.swing.JTable.editingStopped(JTable.java:2989)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.
java:112)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultC
ellEditor.java:242)
at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:
176)
at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:3308)
at javax.swing.JTable.editCellAt(JTable.java:2359)
at javax.swing.plaf.basic.BasicTableUI$MouseInputHandler.mousePressed(Ba
sicTableUI.java:464)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:22
1)
at java.awt.Component.processMouseEvent(Component.java:3714)
at java.awt.Component.processEvent(Component.java:3546)
at java.awt.Container.processEvent(Container.java:1167)
at java.awt.Component.dispatchEventImpl(Component.java:2595)
at java.awt.Container.dispatchEventImpl(Container.java:1216)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2458
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2217)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2132)
at java.awt.Container.dispatchEventImpl(Container.java:1203)
at java.awt.Window.dispatchEventImpl(Window.java:918)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:336)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:134)
我把public Class getColumnClass(int col) 除掉后,则修改图框内容不会发生异常,但记录的最后一项“boolean"会显示为字符串,而不是图框形式。
请高手释疑!!!!谢谢!!!
|
将
data[row][col]=new Integer((String)value);
改为
data[row][col]=value;
data[row][col]=new Integer((String)value);
改为
data[row][col]=value;