JTable删除行出问题
String[]columnNames={"State","TaskName","Warning","Priority"};ImageIconwic=newImageIc...
String[] columnNames = { "State", "TaskName", "Warning", "Priority" };
ImageIcon wic = new ImageIcon("src/res/remind.png");
ImageIcon nwic = new ImageIcon("src/res/noRemind.png");
ImageIcon pic1 = new ImageIcon("src/res/lowPriority.png");
ImageIcon pic2 = new ImageIcon("src/res/mediumPriority.png");
ImageIcon pic3 = new ImageIcon("src/res/highPriority.png");
Object[][] rowData = { { false, "吃饭", nwic, pic1 },
{ false, "睡觉", wic, pic2 }, { false, "锻炼", nwic, pic3 },
{ false, "打字", wic, pic1 }, { false, "唱K", nwic, pic2 },
{ false, "跳舞", wic, pic2 }, { false, "跑步", wic, pic2 },
{ false, "游泳", nwic, pic2 }, { false, "竞技", wic, pic2 }
};
tableModel = new MyTableModel(rowData, columnNames);
taskTable = new JTable(tableModel);
// 使用ListSelectionModel模型监听行的选择事件
ListSelectionModel selectionModel = taskTable.getSelectionModel();
selectionModel.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent le) {
if(taskTable.getSelectedColumn()==0){
if(taskTable.getSelectedRow()>=0){
tableModel.removeRow(taskTable.getSelectedRow());
}
}
}
});
现在问题是删除一行后,它下面的行浮上来并且第一列的单选框中也被选中。
然后依次删除其他行就出现一个这样的异常:
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
望碰到过类似的问题的童鞋帮忙支招,这里先投100分。解决后另有重赏!
二楼注意讲话,首先声明我开始是想把所有代码贴上来的,但是显示超过字数。所以就捡关键代码贴了。我就再贴一张图吧。
MyTableModel继承了DefaultTableModel
public class MyTableModel extends DefaultTableModel{
public MyTableModel(){
}
public MyTableModel(Object[][] data, Object[] columnNames){
super(data, columnNames);
}
public Class getColumnClass(int column){
return getValueAt(0, column).getClass();
}
public boolean isCellEditable(int row, int column){
Class columnClass = getColumnClass(column);
return columnClass == Boolean.class;
}
}
我把打字这一行删除后就出现下面的图片!请看图! 展开
ImageIcon wic = new ImageIcon("src/res/remind.png");
ImageIcon nwic = new ImageIcon("src/res/noRemind.png");
ImageIcon pic1 = new ImageIcon("src/res/lowPriority.png");
ImageIcon pic2 = new ImageIcon("src/res/mediumPriority.png");
ImageIcon pic3 = new ImageIcon("src/res/highPriority.png");
Object[][] rowData = { { false, "吃饭", nwic, pic1 },
{ false, "睡觉", wic, pic2 }, { false, "锻炼", nwic, pic3 },
{ false, "打字", wic, pic1 }, { false, "唱K", nwic, pic2 },
{ false, "跳舞", wic, pic2 }, { false, "跑步", wic, pic2 },
{ false, "游泳", nwic, pic2 }, { false, "竞技", wic, pic2 }
};
tableModel = new MyTableModel(rowData, columnNames);
taskTable = new JTable(tableModel);
// 使用ListSelectionModel模型监听行的选择事件
ListSelectionModel selectionModel = taskTable.getSelectionModel();
selectionModel.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent le) {
if(taskTable.getSelectedColumn()==0){
if(taskTable.getSelectedRow()>=0){
tableModel.removeRow(taskTable.getSelectedRow());
}
}
}
});
现在问题是删除一行后,它下面的行浮上来并且第一列的单选框中也被选中。
然后依次删除其他行就出现一个这样的异常:
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
望碰到过类似的问题的童鞋帮忙支招,这里先投100分。解决后另有重赏!
二楼注意讲话,首先声明我开始是想把所有代码贴上来的,但是显示超过字数。所以就捡关键代码贴了。我就再贴一张图吧。
MyTableModel继承了DefaultTableModel
public class MyTableModel extends DefaultTableModel{
public MyTableModel(){
}
public MyTableModel(Object[][] data, Object[] columnNames){
super(data, columnNames);
}
public Class getColumnClass(int column){
return getValueAt(0, column).getClass();
}
public boolean isCellEditable(int row, int column){
Class columnClass = getColumnClass(column);
return columnClass == Boolean.class;
}
}
我把打字这一行删除后就出现下面的图片!请看图! 展开
2个回答
展开全部
你要设定指针的,JAVA里面JTable不会自动返回,永远删的都会是第一行,也就是数据库里的第一调数据
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询