0

I have a program with an editable JTable. I am attempting to create a key binding for the table that will delete the rows selected by the user when the Delete key is pressed. This is the code for my key binding:

inventoryTable.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
inventoryTable.getActionMap().put("delete", new RemoveSelectedAction());

However, when I press the Delete key while one or more rows are selected, no rows are deleted, and the selected cell displays its editor.

Having looked at the answers here and here, I suspect the problem is this: because my table is editable, the selected cell is intercepting the keypress before it reaches the JTable's key binding.

  1. Am I correct in my assessment of why the Delete key is not behaving as desired?
  2. How do I get the keypress to reach the JTable's binding?

UPDATE: When I changed my table model's isCellEditable method to return false for all cells, the key binding worked as expected, so I am now almost certain that the problem is the keypress being intercepted by the selected cell.

This is the code that produces the problem:

import javax.swing.*;
import java.awt.event.*;

public class TableDeleteRowsTest extends JFrame
{
   public TableDeleteRowsTest()
   {
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      String[] columnNames = { "A", "B", "C" };
      Object[][] data = {
      { "foo", "bar", "baz" },
      { 1, 2, 3 }
      };
      JPanel contentPane = new JPanel();
      JTable table = new JTable(data, columnNames);
      table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
      KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
      table.getActionMap().put("delete", new AbstractAction()
         {
            public void actionPerformed(ActionEvent e)
            {
               System.out.println("In the actual program, this will delete the rows currently selected by the user.");
            }
         }); 
      contentPane.add(table);
      contentPane.setOpaque(true);
      setContentPane(contentPane);
   }

   public static void createAndShowGUI()
   {
      TableDeleteRowsTest test = new TableDeleteRowsTest();
      test.pack();
      test.setVisible(true);
   }

   public static void main(String[] args)
   {
      SwingUtilities.invokeLater(new Runnable()
         {
            public void run()
            {
               createAndShowGUI();
            }
         });
   }
}
train1855
  • 300
  • 2
  • 5
  • 14
  • For best help, please create and post for us a valid [mcve]. – Hovercraft Full Of Eels Nov 02 '17 at 22:43
  • What does `RemoveSelectedAction` do? – MadProgrammer Nov 02 '17 at 22:48
  • What if you use a different InputMap, say `inventoryTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)`? This appears to be the appropriate map to use for "composite" components, like a JTable as per the [Key Bindings tutorial](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html). – Hovercraft Full Of Eels Nov 02 '17 at 22:49
  • 1
    [And a working example for your comparison](https://stackoverflow.com/questions/25070511/add-jbutton-to-each-row-of-a-jtable/25071138#25071138) – MadProgrammer Nov 02 '17 at 22:50
  • So the problem and its solution are just as I've mentioned -- you're using the wrong InputMap. – Hovercraft Full Of Eels Nov 02 '17 at 23:17
  • @HovercraftFullOfEels Which is not what is addressed by the answer you marked this question as a duplicate of. That question is about various methods of deleting rows, with a key binding as only one of the options, and my actual issue (which input map to use) is buried deep in the example and never explicitly stated. This question is specifically about what input map to use and deserves to have its own answer that explicitly states the solution to my specific problem. – train1855 Nov 02 '17 at 23:21
  • You do have a point there, but I'm also a bit surprised that there was no mention of thanks to pointing out the problem with your code and its simple solution. – Hovercraft Full Of Eels Nov 02 '17 at 23:22
  • I've used another duplicate to help close this question where one answer (not mine) mentions using this input map for this sort of situation. – Hovercraft Full Of Eels Nov 02 '17 at 23:25
  • @HovercraftFullOfEels Okay. That answer should be a better help to future viewers of this question. As an aside, do you have any suggestions for how to use search terms that will actually find these things without having to ask and then be pointed to them? – train1855 Nov 02 '17 at 23:29
  • you're welcome, .... I think – Hovercraft Full Of Eels Nov 02 '17 at 23:29

0 Answers0