How to make delete button to delete rows in JTable?

前端 未结 3 884
暗喜
暗喜 2021-01-24 10:05

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 t

相关标签:
3条回答
  • 2021-01-24 10:10

    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.

    0 讨论(0)
  • 2021-01-24 10:11
    Try This:
    
     MyTableModel mymodel=(MyTableModel)table.getModel();
                    int rowcount= mymodel.getRowCount();
                    for(int i = 0;i<rowcount;i++){
                        mymodel.removeRow(0);
                    }
    
    0 讨论(0)
  • 2021-01-24 10:21

    To disable the automatic edition mode try this :

    table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
    
    0 讨论(0)
提交回复
热议问题