5

I am having some trouble removing the default input map information on my components in a java swing application. This is what I am trying to do:

//List of keys to remove
public static final int[] OVERWRITTEN_KEYS = 
{
    VK_SPACE
};

//Get default input maps
InputMap[] im = { 
    (InputMap)UIManager.get("Button.focusInputMap"),
    (InputMap)UIManager.get("ToggleButton.focusInputMap"),
    (InputMap)UIManager.get("Slider.focusInputMap"),
    (InputMap)UIManager.get("RadioButton.focusInputMap"),
    (InputMap)UIManager.get("TextArea.focusInputMap"),
    (InputMap)UIManager.get("TextField.focusInputMap")
};

//Loop through input maps        
for(int i = 0; i < im.length; i++)
{
    //Loop through keys
    for(int j = 0; j < OVERWRITTEN_KEYS.length; j++)
    {
        if(im[i] != null)
        {
            //Overwrite press and release of button
            im[i].put(KeyStroke.getKeyStroke(OVERWRITTEN_KEYS[j],0,false), "none");
            im[i].put(KeyStroke.getKeyStroke(OVERWRITTEN_KEYS[j],0,true), "none");
        }
    }
}

But, for some reason, this has no effect. Pressing the spacebar still fires a JButton click, etc. Does anyone see something wrong with this code block? Thanks beforehand.

SuperTron
  • 4,203
  • 6
  • 35
  • 62

1 Answers1

8

I'm having trouble reproducing the problem you describe. I usually modify the component's InputMap, but the UIManager instance has the default bindings. In the example below,

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), NIL);

effectively blocks the Space key from invoking the button's ActionListener. Uncommenting the line

button.getActionMap().put(NIL, nil);

associates the Space key with an effectively empty action, as shown in the doNothing action described in How to Make and Remove Key Bindings.

/**
 * @see http://stackoverflow.com/q/12133795/230513
 */
public class NilBindingTest extends JPanel {

    private static final String NIL = "none";
    private Action nil = new AbstractAction(NIL) {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("\"" + e.getActionCommand() + "\"");
        }
    };
    private JButton button = new JButton(nil);
    //private InputMap im = button.getInputMap();
    private InputMap im = (InputMap) UIManager.get("Button.focusInputMap");

    public NilBindingTest() {
        this.add(new JButton("foo"));
        System.out.println(Arrays.toString(im.keys()));
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), NIL);
        //button.getActionMap().put(NIL, nil);
        this.add(button);
    }

    private void display() {
        JFrame f = new JFrame("NilBindingTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

            @Override
            public void run() {
                new NilBindingTest().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Well, I tried this, but it turns out that my UI manager is returning a null when I call `UIManager.get("Button.focusInputMap");`. Any ideas on why that could be the case? – SuperTron Aug 29 '12 at 03:30
  • I should probably mention that I am using a custom synth look and feel, generated from an xml file I wrote.... – SuperTron Aug 29 '12 at 03:32
  • Sorry, no idea how to add a new `focusInputMap` using XML. – trashgod Aug 29 '12 at 10:28
  • So is there no way to remove a key from every components input map except looping through all the components and doing it one at a time then? – SuperTron Aug 29 '12 at 13:24
  • I guess I don't understand why your custom L&F returns `null` for the `focusInputMap` defaults, when the original L&F has them. – trashgod Aug 29 '12 at 17:38
  • Well, I suppose that is the premise for another question then. I'm going to mark this answer as correct because it works for a non-synth look and feel :) Thanks. – SuperTron Aug 29 '12 at 20:17
  • Good idea. There are several regular Swing followers who know much more about synth that I do. Don't hesitate to cite this Q&A going forward. Be prepared for some push-back on user acceptance, though. :-) – trashgod Aug 29 '12 at 20:52
  • nice example :-) Just a note: astonishingly doesn't work for Nimbus - see @SuperTron 's (follow-up question)[http://stackoverflow.com/q/12464180/203657] – kleopatra Sep 21 '12 at 09:29
  • @kleopatra: Than you for ping. Oddly, this works on Mac OS X with `com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel`. – trashgod Sep 21 '12 at 10:35