删除 JTable 最后一行时出现问题

发布于 2024-10-04 12:14:03 字数 3953 浏览 3 评论 0原文

这是我第一次在这里提问,所以如果有什么不合适的地方请原谅我,如果我的英语不是很好,请原谅。

嗯,简而言之,目前我正在使用 Swing 开发一个 Java 桌面应用程序,但在使用 table 时遇到问题。我有行,每行都有一个用于删除该行的按钮。一切都很好(我可以毫无问题地删除行),直到我尝试删除最后一行。最后一行可以删除,但显然有一个例外,如下所示:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:648)
    at javax.swing.JTable.setValueAt(JTable.java:2710)
    at javax.swing.JTable.editingStopped(JTable.java:4712)
    at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:125)

从堆栈跟踪来看,“4”似乎是我之前删除的最后一行索引,但我不知道如何处理这个问题。我已经在寻找解决方案,但仍然无法解决。请注意,当我删除最后一行时,仍然有其他行,之后我无法删除其他行。当我单击删除按钮时,也出现了同样的异常。

哦。我使用 DefaultTableModel 中的removeRow() 来删除该行。任何帮助将不胜感激。谢谢。

编辑 这些是我使用的代码。

public class ViewDetail extends JInternalFrame implements ActionListener {
    ...
    String column[] = { "Aaa", "Bbb", "Delete This"};

    tableModel = new DefaultTableModel();
    Object row[][] = new Object[size][column.length];

    //fill the data from db
    int r = 0;
    for (int i = 0; i < size; i++) {
    row[r][0] = ...;
    row[r][1] = ...;
    row[r][2] = "Delete";
    r++;
    }

    tableModel.setDataVector(row, column);
    table = new JTable(tableModel);

    tableColumn = table.getColumn("Aaa");
    tableColumn.setPreferredWidth(75);
    tableColumn = table.getColumn("Bbb");
    tableColumn.setPreferredWidth(75);
    tableColumn = table.getColumn("Delete This");
    tableColumn.setPreferredWidth(75);
    tableColumn.setCellRenderer(new ButtonRenderer());
    tableColumn.setCellEditor(new ButtonEditor(new JCheckBox());
     ...
}

public class ButtonRenderer extends JButton implements TableCellRenderer {
    public ButtonRenderer() {
        setOpaque(true);
    }
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(UIManager.getColor("Button.background"));
        }
        setText((value == null) ? "" : value.toString());
        return this;
    }
}

public class ButtonEditor extends DefaultCellEditor {
    protected JButton button;
    private String label;
    private boolean isPushed;
    private JTable table;
    public ButtonEditor(JCheckBox checkBox) {
    super(checkBox);
    button = new JButton();
    button.setOpaque(true);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            fireEditingStopped();
            }
        });
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {
    if (isSelected) {
        button.setForeground(table.getSelectionForeground());
        button.setBackground(table.getSelectionBackground());
    } else {
        button.setForeground(table.getForeground());
        button.setBackground(table.getBackground());
    }
    label = (value == null) ? "" : value.toString();
    button.setText(label);
    isPushed = true;
    this.table = table;
    return button;
    }

    public Object getCellEditorValue() {
    if (isPushed) {
                  DefaultTableModel tableModel = (DefaultTableModel) table.getModel();                            
                  tableModel.removeRow(table.getSelectedRow());                
            }
            isPushed = false;
    return new String(label);
    }

    public boolean stopCellEditing() {
    isPushed = false;
    return super.stopCellEditing();
}

protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
}

This is my first time asking here so forgive me if something isn't appropriate, and sorry if my English isn't very good.

Well, to make it short, currently I'm developing a Java desktop app with Swing and I have a problem using table. I have rows with each row have a button to delete the row. Everything is okay (i can delete rows with no problem) until i try to delete the last row. The last row can be deleted but apparently there is an exception, something like this:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:648)
    at javax.swing.JTable.setValueAt(JTable.java:2710)
    at javax.swing.JTable.editingStopped(JTable.java:4712)
    at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:125)

From the stack trace, it seems that "4" is my previously deleted last row index, but i have no idea how to deal with this. I already search for solution but i still can't solve it. Note that there are still other rows when i delete the last row, and after that i can't delete the others rows. The same exception also resulted when i click the delete button.

Oh. and I use removeRow() from DefaultTableModel to delete the row. Any help will be appreciated. Thanks.

EDIT
These are the code I use.

public class ViewDetail extends JInternalFrame implements ActionListener {
    ...
    String column[] = { "Aaa", "Bbb", "Delete This"};

    tableModel = new DefaultTableModel();
    Object row[][] = new Object[size][column.length];

    //fill the data from db
    int r = 0;
    for (int i = 0; i < size; i++) {
    row[r][0] = ...;
    row[r][1] = ...;
    row[r][2] = "Delete";
    r++;
    }

    tableModel.setDataVector(row, column);
    table = new JTable(tableModel);

    tableColumn = table.getColumn("Aaa");
    tableColumn.setPreferredWidth(75);
    tableColumn = table.getColumn("Bbb");
    tableColumn.setPreferredWidth(75);
    tableColumn = table.getColumn("Delete This");
    tableColumn.setPreferredWidth(75);
    tableColumn.setCellRenderer(new ButtonRenderer());
    tableColumn.setCellEditor(new ButtonEditor(new JCheckBox());
     ...
}

public class ButtonRenderer extends JButton implements TableCellRenderer {
    public ButtonRenderer() {
        setOpaque(true);
    }
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(UIManager.getColor("Button.background"));
        }
        setText((value == null) ? "" : value.toString());
        return this;
    }
}

public class ButtonEditor extends DefaultCellEditor {
    protected JButton button;
    private String label;
    private boolean isPushed;
    private JTable table;
    public ButtonEditor(JCheckBox checkBox) {
    super(checkBox);
    button = new JButton();
    button.setOpaque(true);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            fireEditingStopped();
            }
        });
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {
    if (isSelected) {
        button.setForeground(table.getSelectionForeground());
        button.setBackground(table.getSelectionBackground());
    } else {
        button.setForeground(table.getForeground());
        button.setBackground(table.getBackground());
    }
    label = (value == null) ? "" : value.toString();
    button.setText(label);
    isPushed = true;
    this.table = table;
    return button;
    }

    public Object getCellEditorValue() {
    if (isPushed) {
                  DefaultTableModel tableModel = (DefaultTableModel) table.getModel();                            
                  tableModel.removeRow(table.getSelectedRow());                
            }
            isPushed = false;
    return new String(label);
    }

    public boolean stopCellEditing() {
    isPushed = false;
    return super.stopCellEditing();
}

protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆光下的微笑 2024-10-11 12:14:03

Swing 正在尝试将新值设置到您删除的行中!尝试将删除代码移至 Runnable 中,并使用 swing 实用程序类中的 invokeLater 来执行它。

Swing is trying to set the new value into the row you remove! Try moving the remove code into a Runnable and use invokeLater in the swing utility class to execute it.

甜是你 2024-10-11 12:14:03

你能发布你在 JTable 中初始化、添加元素和删除的代码吗?

我的第一个猜测是你要么犯了经典错误
“我的表中有 4 个元素,因此要删除我删除的最后一个元素(4)”,当您的表索引从 0 变为 3

,或者您尝试在同一循环中删除多个对象时,就像

  for(int i = 0; i<lenght; i++){
       if(i%2==0){//every even number
         tab.remove(i);
        }
    }

您正在修改大小一样在您的表格中,元素已移动,但您在删除时没有考虑到它。

这些只是猜测/指向可能导致问题的原因。如果这不能解决问题,请发布您的代码

could you post the code where you init, add elements and delete in your JTable?

My first guess is that you are either doing the classical mistake
"I have 4 elements in my table so to delete the last one I remove(4)" when you your table goes from 0 to 3 in indices

or you are trying to remove several object in the same loop like

  for(int i = 0; i<lenght; i++){
       if(i%2==0){//every even number
         tab.remove(i);
        }
    }

then your are modifying the size of your table, the elements are shifted but your not taking it in account when removing.

those are just guesses/pointer to what might cause the problem. please post your code if this doesn't solve it

深海不蓝 2024-10-11 12:14:03

表格按钮列展示了如何将列呈现为按钮以及如何添加单击按钮时执行的操作。示例操作恰好展示了如何删除行。

The Table Button Column shows how you can render a column as a button and how to add an Action that is executed when the button is clicked. The example Action just happens to show how to delete a row.

浪荡不羁 2024-10-11 12:14:03

这里您只需要删除应该迭代的第 0 行

private voidfreshTable() {
int rowCount= model.getRowCount();
// System.out.println(rowCount);

   for(int i=0;i<rowCount;i++ ){
        model.removeRow(0);
        //System.out.println(i);
   }

}

Here you need to remove only 0 th row that should be iterated

private void refreshTable() {
int rowCount= model.getRowCount();
// System.out.println(rowCount);

   for(int i=0;i<rowCount;i++ ){
        model.removeRow(0);
        //System.out.println(i);
   }

}
九公里浅绿 2024-10-11 12:14:03

您可以添加新的变量并检查 fireEditingStopped() :

public class ButtonEditor extends DefaultCellEditor {

//create new variable 
private boolean isDeleteRow = false;
/////////////////

protected JButton button;
private String label;
private boolean isPushed;
private JTable table;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        fireEditingStopped();
        }
    });
}

public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int column) {
if (isSelected) {
    button.setForeground(table.getSelectionForeground());
    button.setBackground(table.getSelectionBackground());
} else {
    button.setForeground(table.getForeground());
    button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
this.table = table;

// set false when click to button
isDeleteRow = false; 
/////////////////
return button;
}

public Object getCellEditorValue() {
if (isPushed) {

              // set true when isPushed button
              isDeleteRow = true;   
              /////////////////

        }
        isPushed = false;
return new String(label);
}

public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}

protected void fireEditingStopped() {
    super.fireEditingStopped();

     //check if isDeleteRow, remove row
     if(isDeleteRow)
     {
          DefaultTableModel tableModel = (DefaultTableModel) table.getModel();                            
          tableModel.removeRow(table.getSelectedRow());   
     }
     /////////////////
}
}

you can add new vaiable and check to fireEditingStopped() :

public class ButtonEditor extends DefaultCellEditor {

//create new variable 
private boolean isDeleteRow = false;
/////////////////

protected JButton button;
private String label;
private boolean isPushed;
private JTable table;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        fireEditingStopped();
        }
    });
}

public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int column) {
if (isSelected) {
    button.setForeground(table.getSelectionForeground());
    button.setBackground(table.getSelectionBackground());
} else {
    button.setForeground(table.getForeground());
    button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
this.table = table;

// set false when click to button
isDeleteRow = false; 
/////////////////
return button;
}

public Object getCellEditorValue() {
if (isPushed) {

              // set true when isPushed button
              isDeleteRow = true;   
              /////////////////

        }
        isPushed = false;
return new String(label);
}

public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}

protected void fireEditingStopped() {
    super.fireEditingStopped();

     //check if isDeleteRow, remove row
     if(isDeleteRow)
     {
          DefaultTableModel tableModel = (DefaultTableModel) table.getModel();                            
          tableModel.removeRow(table.getSelectedRow());   
     }
     /////////////////
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文