Jan Beneš
Jan Beneš

Reputation: 722

How to setup key bindings that processes all number strokes in one event?

I've registered key bindings to button and I'd like to react to all number key-strokes. I could register different event to every single key(0-9), but that's kind of stupid. So is it possible to handle it all in one event?

Here is my code that reacts only to key 0 on numpad:

  private void setKeyBindings() {
    AbstractAction aa = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            System.out.println("Here");
        }
    };

    this.editButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), "0");
    this.editButton.getActionMap().put("0", aa);
}

Thanks

Upvotes: 2

Views: 268

Answers (2)

camickr
camickr

Reputation: 324118

So is it possible to handle it all in one event?

You can create one event listener to be used by all bindings:

Maybe something like:

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

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
//              display.setCaretPosition( display.getDocument().getLength() );
            System.out.println(e.getActionCommand());
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(30, 30) );
            buttonPanel.add( button );

            InputMap inputMap = buttonPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(KeyStroke.getKeyStroke(text), text);
            inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
            buttonPanel.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

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

Upvotes: 3

MadProgrammer
MadProgrammer

Reputation: 347204

The question you know have, is how to recognise what key stroke the action is responding to (or why the action was called)

Rather then using a single instance of an action, you could create a single special action which you could seed with the information it needs in order to do its job

public class NumberAction extends AbstractAction {
    private int number;

    public NumberAction(int number) {
        this.number = number
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("Here");
    }
}

Then, you would create them as you need....

private void setKeyBindings() {
    this.editButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), "0");
    this.editButton.getActionMap().put("0", new NumberAction(0));
    //Other numbers...
}

Upvotes: 2

Related Questions