How to make delete button to delete rows in JTable

2020-04-20 11:53发布

I tried to make keystroke Delete but when I select cell and press delete button it enter in the selected cell instead to delete the whole row . How I can disable Delete button to do nothing when cell is selected?


noo. I have delete button on my GUI but I want to make keyboard shortcut to this button. I want to make delete button on my keyboard to call delete method. But when I press it when cell is selected it enter in the cell instead to delete the entire row

3条回答
女痞
2楼-- · 2020-04-20 12:15

To disable the automatic edition mode try this :

table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
查看更多
Deceive 欺骗
3楼-- · 2020-04-20 12:26

The DefaultCellEditor for many JTable cell types delegates to a JTextField, which binds the Delete key to the delete-next action. To preempt the default behavior, you'll have to remove the existing binding from DefaultCellEditor, as shown in How to Use Key Bindings: How to Make and Remove Key Bindings. You can replace it with your own binding, as shown in this example; a typical DefaultCellEditor is shown here.

查看更多
Anthone
4楼-- · 2020-04-20 12:31
Try This:

 MyTableModel mymodel=(MyTableModel)table.getModel();
                int rowcount= mymodel.getRowCount();
                for(int i = 0;i<rowcount;i++){
                    mymodel.removeRow(0);
                }
查看更多
登录 后发表回答