Results 1 to 3 of 3
  1. #1
    msphelix is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Multiple key bindings: an AbstractAction class for each bound key?

    Hi,
    I am learning key bindings; thru examples I've found I can do what I want but the way I do it seems cumbersome and requires calling the same method 2 separate places.

    I have a JPanel with two JButtons, buttonHello and buttonGoodBye; using standard procedure I have an ActionListener which catches presses on the buttons and performs two totally thrilling actions, sayHello() and sayGoodBye().
    I have created two classes derived from AbstractAction, HKey, its actionPerformed() calls sayHello(), and GKey, its actionPerformed() calls sayGoodBye().
    I have a method that creates an InputMap and an ActionMap; each has two entries: one for the h key/HKey and one for the g key/GKey.
    Everything works fine: pressing buttonHello or the h key calls sayHello(); pressing buttonGoodBye or the g key calls sayGoodBye() (Brownie points to any one who can guess sayHello() and sayGoodBye() do before looking at the code. :0)

    My question is the following: is there a way to do this all in one actionPerformed() no matter which button or key the user presses instead of having 3 actionPerformed()s? If you have to have a separate AbstractAction class for every key you bind, the code could become huge.

    Thanks for your help.

    Here is my JPanel class:
    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    
    public class MainPanel extends JPanel implements ActionListener
    {
    	private InputMap;
    	private ActionMap;
    	private HKey hKey = new HKey();
    	private GKey gKey = new GKey();
        
    	private JButton buttonHello, buttonGoodBye;
    	
    	public MainPanel()
    	{
    		buttonHello = new JButton("Hello");
    		buttonHello.addActionListener(this);
    		
    		buttonGoodBye = new JButton("Good Bye");
    		buttonGoodBye.addActionListener(this);
    		
    		add(buttonHello);
    		add(buttonGoodBye);
    		
    		setPreferredSize(new Dimension(400, 300));
    		
    		createKeyBindings();
    	}
    	
    	private void createKeyBindings()
    	{
    		myInputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
    		myInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_H, 0, false), "h");
    		myInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false), "g");
            
    		myActionMap = getActionMap();
    		myActionMap.put("h", hKey);
    		myActionMap.put("g", gKey);
    	}
    	
    	private void sayHello()
    	{
    		System.out.println("Hello World!");
    	}
    	
    	private void sayGoodBye()
    	{
    		System.out.println("Good Bye, Cruel World!");
    	}
    	
    	final class HKey extends AbstractAction
    	{
    		public void actionPerformed(ActionEvent e) 
    		{
    			sayHello();
    		}
    	}
    
    	final class GKey extends AbstractAction
    	{
    		public void actionPerformed(ActionEvent e) 
    		{
    			sayGoodBye();
    		}
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		Object source = e.getSource();
    		
    		if(source == buttonHello)
    		{
    			sayHello();
    		}
    		else if(source == buttonGoodBye)
    		{
    			sayGoodBye();
    		}
    	}
    }
    Last edited by msphelix; 11-01-2012 at 04:08 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Multiple key bindings: an AbstractAction class for each bound key?

    Please use [code] tags [/code] when posting code.
    Many of us won't read unformatted code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    msphelix is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Multiple key bindings: an AbstractAction class for each bound key?

    Trolls, thank you. I didn't know how to do that; it's fixed now.

Similar Threads

  1. Unsure of whether to use key bindings
    By kjkrum in forum AWT / Swing
    Replies: 1
    Last Post: 04-15-2012, 08:33 AM
  2. can one disable key bindings on a JComponent?
    By gib65 in forum AWT / Swing
    Replies: 2
    Last Post: 10-14-2010, 05:44 PM
  3. JAXB Bindings - error while compiling Schema(s)
    By Stipejava in forum NetBeans
    Replies: 0
    Last Post: 10-30-2009, 01:15 PM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 01:10 PM
  5. genesis 3.0-RC2: Swing, SWT and Thinlet bindings
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •