How can I disable the default action of F10 in a window (viewport menu)?

The F10default action is to show the window menu. However, I would like to disable this feature.

UPDATED: Prerequisites: I would like to implement a special behavior in the JTextField if the user presses any key. Unfortunately the JTextField does not receive the event when clicked F10because it is caught by the window (and the menu is displayed).

Does anyone know how to disable this key binding on a window?

I tried to disable it in the root panel but with no success:

frame.getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");

      

I searched a lot but couldn't find a solution for this problem. Perhaps one of you knows the answer.

UPDATE2 Here's some sample code to reproduce this behavior:

public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {

  @Override
  public void run() {
    JFrame.setDefaultLookAndFeelDecorated(true);

    final JTextField edit = new JTextField();
    edit.setEditable(false);
    edit.addKeyListener(new KeyAdapter() {

      @Override
      public void keyReleased(final KeyEvent ke) {
        edit.setText(KeyEvent.getKeyText(ke.getKeyCode()));
      }
    });

    final JFrame frame = new JFrame("DEMO");
    frame.setSize(320, 240);
    frame.getContentPane().add(edit);
    frame.setVisible(true);
  }
});

      

}

Plase Note: There is a different behavior depending on whether "setDefaultLookAndFeelDecorated" is set to true or false.

Thank you in advance:)

+3


source to share


2 answers


I tried to disable it in the root panel but with no success:

Check the key bindings for binding all Swing components.

You will see that the key is F10bound to JMenuBar

. Therefore, you should be able to use:

menuBar.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");

      



Edit:

The point was missing that you didn't have a menu bar.

It looks like you can't just bind the binding to "none". It looks like Swing is still looking for the tree to find the action to perform. You need to provide a dummy action that does nothing:

Action action = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("do nothing");
    }
};

JPanel content = (JPanel)frame.getContentPane();
String key = "F10";
KeyStroke f10 = KeyStroke.getKeyStroke( key );
frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(f10, key);
frame.getRootPane().getActionMap().put(key, action);

      

+3


source


If I followed your action correctly, create a Keypressed event for your JTextField in netbeans by putting the following code



private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       

   // Add below
       int key = evt.getKeyCode(); 

           if (evt.getSource() == jTextField1)
           {
               if (key == KeyEvent.VK_F10)
               {
                  // your actions here
                   System.out.println("Hello I am f10");

               }

           }
         //   end of if

        } 

      

0


source







All Articles