4 Replies Latest reply on Sep 7, 2010 1:07 PM by 843807

    JTable starts editing when F3 is pressed - howto disable?

    843807
      Hi,

      I would like to add some shortcuts to my application (F3, F4, F5, ...), however when the JTable has focus and JTable's cursor is on a editable cell, it starts the editing process.
      Is there a way to tell the JTable to "ignore" some keys?

      Thank you in advance, Clemens
        • 1. Re: JTable starts editing when F3 is pressed - howto disable?
          843807
          Use JComponent.getInputMap(int condition).remove(KeyStroke).

          There are pitfalls in removing/changing the default action of KeyStrokes. Users may be surprised/upset if they are used to using a KeyStroke that you have changed. Also, the registered KeyStrokes are not going to be consistent across LookAndFeels.
          • 2. Re: JTable starts editing when F3 is pressed - howto disable?
            843807
            Hi Aephyr,
            Use JComponent.getInputMap(int condition).remove(KeyStroke).
            Thats exactly what I've tried before, but it doesn't work - as soon as I press F3 editing starts :/

            I also tried registering a dummy action, to replace what might be there :
                 AbstractAction disabled = new AbstractAction()
                 {
                     public boolean isEnabled() { return false; } 
                     public void actionPerformed(ActionEvent e) { }
                 };
            
                 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "none");
                 getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "none");
                 getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "none");
                 getActionMap().put("none", disabled);
            Any idea why JTable behaves so different from all other components in this case?


            Thank you in advance, Clemens
            getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).remove(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
            getInputMap(JComponent.WHEN_FOCUSED).remove(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
            • 3. Re: JTable starts editing when F3 is pressed - howto disable?
              walterln
              It behaves differently because it tries to pass key strokes to the editor and overrides processKeyBinding to that end. Try:
              import java.awt.EventQueue;
              import java.awt.event.KeyEvent;
              import java.util.EventObject;
              
              import javax.swing.JFrame;
              import javax.swing.JScrollPane;
              import javax.swing.JTable;
              import javax.swing.table.DefaultTableModel;
              
              public class TestJTable {
                  public static void main(String[] args) {
                      EventQueue.invokeLater(new Runnable() {
                          public void run() {
                              JTable table = new JTable(new DefaultTableModel(1, 1)) {
                                  @Override
                                  public boolean editCellAt(int row, int column, EventObject e) {
                                      if (e instanceof KeyEvent) {
                                          int code = ((KeyEvent) e).getKeyCode();
                                          int start = KeyEvent.VK_F1;
                                          int end = KeyEvent.VK_F21;
                                          if (code != KeyEvent.VK_F2
                                                  && code >= start && code <= end) {
                                              return false;
                                          }
                                      }
                                      return super.editCellAt(row, column, e);
                                  }
                              };
                              JFrame frame = new JFrame("Test");
                              frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                              frame.getContentPane().add(new JScrollPane(table));
                              frame.pack();
                              frame.setLocationRelativeTo(null);
                              frame.setVisible(true);
                          }
                      });
                  }
              }
              Actually JTable does check if the key press triggers an action, but that means it only works if you install the key binding on the table input map with WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.
              • 4. Re: JTable starts editing when F3 is pressed - howto disable?
                843807
                Thanks Walter :)

                I decided not to use function keys, but Ctrl+N/U.., so I now simply check wether the CTRL-Bit is set in the modifiers.
                Works like a charm, thanks you very much again.

                - Clemens