当前位置: 技术问答>java相关
JTable的小问题,谢谢
来源: 互联网 发布时间:2015-02-25
本文导语: 运行程序,当age列的某一格得到焦点后,单击age或married的某一格,弹出Exception occurred during event dispatching: java.lang.ClassCastException: java.lang.Integer at MyTableModel.setValueAt(JTableTest.java:52) at javax.swi...
运行程序,当age列的某一格得到焦点后,单击age或married的某一格,弹出Exception occurred during event dispatching:
java.lang.ClassCastException: java.lang.Integer
at MyTableModel.setValueAt(JTableTest.java:52)
at javax.swing.JTable.setValueAt(JTable.java:1734)
at javax.swing.JTable.editingStopped(JTable.java:2989)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.
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.setVisible(true);
}
public JTableTest(){
JTable jt=new JTable(new MyTableModel());
jt.setRowHeight(20);
getContentPane().add(new JScrollPane(jt));
}
}
class MyTableModel extends AbstractTableModel{
private String[] column={"name","age","married"};
private Object[][] data=
{{"wang",new Integer(28),new Boolean(true)},
{"chen",new Integer(26),new Boolean(false)},
{"chang",new Integer(35),new Boolean(true)},
{"lin",new Integer(18),new Boolean(false)}};
public int getColumnCount(){
return (column.length);
}
public int getRowCount(){
return (data.length);
}
public String getColumnName(int col){
return (column[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);
}
}
}
java.lang.ClassCastException: java.lang.Integer
at MyTableModel.setValueAt(JTableTest.java:52)
at javax.swing.JTable.setValueAt(JTable.java:1734)
at javax.swing.JTable.editingStopped(JTable.java:2989)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.
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.setVisible(true);
}
public JTableTest(){
JTable jt=new JTable(new MyTableModel());
jt.setRowHeight(20);
getContentPane().add(new JScrollPane(jt));
}
}
class MyTableModel extends AbstractTableModel{
private String[] column={"name","age","married"};
private Object[][] data=
{{"wang",new Integer(28),new Boolean(true)},
{"chen",new Integer(26),new Boolean(false)},
{"chang",new Integer(35),new Boolean(true)},
{"lin",new Integer(18),new Boolean(false)}};
public int getColumnCount(){
return (column.length);
}
public int getRowCount(){
return (data.length);
}
public String getColumnName(int col){
return (column[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);
}
}
}
|
将
data[row][col]=new Integer((String)value);
改为
data[row][col]=value;
错误的原因是个cell的类已经是int类型,无需转换了
data[row][col]=new Integer((String)value);
改为
data[row][col]=value;
错误的原因是个cell的类已经是int类型,无需转换了