1

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

casperOne
  • 73,706
  • 19
  • 184
  • 253
user1761818
  • 365
  • 1
  • 7
  • 14
  • In ActionListener check if cell is selected and if it is just exit function. – Blood Nov 05 '12 at 16:35
  • Before posting a question make sure you have [thoroughly searched](http://stackoverflow.com/questions/how-to-ask) for an answer and your question is different from those already posted on the forum. There are many related questions to your problem under the **Related** links to the right pane. Anyways, [This](http://stackoverflow.com/questions/6462842/how-to-remove-a-row-in-jtable-via-pressing-on-delete-on-the-keyboard) link seems to be more relevant to your query. – Mohsin Nov 05 '12 at 19:04

3 Answers3

2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0
Try This:

 MyTableModel mymodel=(MyTableModel)table.getModel();
                int rowcount= mymodel.getRowCount();
                for(int i = 0;i<rowcount;i++){
                    mymodel.removeRow(0);
                }
BloodRed
  • 38
  • 7
0

To disable the automatic edition mode try this :

table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
EJ SMITH
  • 1
  • 1