Results 1 to 2 of 2
  1. #1
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Default KeyBinds work, focus unfortunatly does not

    I researched quite a lot this time and found some great topics, however I can't seem to get it working for me.
    My keybinds work but only after I clicked on one of the JButtons in my JPanel. First I gave the JPanel focus with .setFocusable(true); and .requestFocusInWindow(); but it didn't work. I had to manually click on a button first.

    Scenario: JFrame pops up and I should be able to immediately use the arrow keys to move a character without clicking on a button first.

    EDIT: mmmk, it seems to be working in this test class but not in my real application.

    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.KeyStroke;
    
    
    public class Test extends javax.swing.JFrame implements ActionListener{
    
    public static void main (String[] args){
     java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
              Test test1 = new Test();
    
             }
          });
    }
    
    private JTextArea txtaTest;
    private JPanel pnlMoveButtons;
    private JButton btnUp, btnDown, btnRight, btnLeft;
    
    public Test(){
    super();
    initGUI();
    }
    
    private void initGUI() {
    	setLocationRelativeTo(null);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setVisible(true);
    
    	setSize(1024, 800);
    	{
    		txtaTest = new JTextArea();
    		txtaTest.setEditable(false);
    		txtaTest.setBounds(790, 120, 180, 400);
    		txtaTest.setText("");
    		getContentPane().add(txtaTest);
    	}
    			{
    				btnUp = new JButton("Up");
    				//btnUp.setBounds(620, 120, 70, 70);
    				btnUp.requestFocusInWindow();
    				btnUp.addActionListener(this);
    			}
    			{
    				btnDown = new JButton("Down");
    				//btnDown.setBounds(620, 195, 70, 70);
    				btnDown.addActionListener(this);
    			}
    			{
    				btnLeft = new JButton("Left");
    				//btnLeft.setBounds(695, 195, 70, 70);
    				btnLeft.addActionListener(this);
    			}
    			{
    				btnRight = new JButton("Right");
    				//btnRight.setBounds(545, 195, 70, 70);
    				btnRight.addActionListener(this);
    			}
    {
    				pnlMoveButtons = new JPanel();
    				pnlMoveButtons.add(new JLabel(""));
    				pnlMoveButtons.add(btnUp);
    				pnlMoveButtons.add(new JLabel(""));
    				pnlMoveButtons.add(btnLeft);
    				pnlMoveButtons.add(btnDown);
    				pnlMoveButtons.add(btnRight);
    				
    				getContentPane().add(pnlMoveButtons);
    				GridLayout grid = new GridLayout(2,3,5,5);
    				pnlMoveButtons.setLayout(grid);
    				pnlMoveButtons.setBounds(545,120, 225, 150);
    			}
    			
    			int cond = JComponent.WHEN_IN_FOCUSED_WINDOW;
    			pnlMoveButtons.getInputMap(cond).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
    			pnlMoveButtons.getInputMap(cond).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");
    			pnlMoveButtons.getInputMap(cond).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
    			pnlMoveButtons.getInputMap(cond).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
    			
    			pnlMoveButtons.getActionMap().put("up", new AbstractAction(){
    				public void actionPerformed(ActionEvent e){
    					btnUp.doClick();
    				}
    			});
    			pnlMoveButtons.getActionMap().put("down", new AbstractAction(){
    				public void actionPerformed(ActionEvent e){
    					btnDown.doClick();
    				}
    			});
    			pnlMoveButtons.getActionMap().put("left", new AbstractAction(){
    				public void actionPerformed(ActionEvent e){
    					btnLeft.doClick();
    				}
    			});
    			pnlMoveButtons.getActionMap().put("right", new AbstractAction(){
    				public void actionPerformed(ActionEvent e){
    					btnRight.doClick();
    				}
    			});
    }
    
    public void actionPerformed(ActionEvent evt){
    		if(evt.getActionCommand() == "Up"){
    			txtaTest.append("\nUp");
    		}
    		if(evt.getActionCommand() == "Down"){
    			txtaTest.append("\nDown");
    		}
    		if(evt.getActionCommand() == "Right"){
    			txtaTest.append("\nRight");
    		}
    		if(evt.getActionCommand() == "Left"){
    			txtaTest.append("\nLeft");
    		}
    }
    }
    Last edited by Bloitz; 05-11-2011 at 08:07 PM.

  2. #2
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Default

    Solved it. I added randomComponent.setFocusable(false) on most other components in the JFrame. Now it works.

    Seriously, 4 hours of reading and trying stuff out, then finally posting it and finding the solution 5 mins later -.-

    Will use test-classes more often now for sure :)

    Link to useful related thread: Unable to capture key pressed :((

Similar Threads

  1. Button without focus
    By comeofage in forum SWT / JFace
    Replies: 0
    Last Post: 04-27-2010, 08:45 AM
  2. Focus scroll bar
    By BlackTea in forum SWT / JFace
    Replies: 0
    Last Post: 02-25-2010, 06:16 PM
  3. How to avoid focus()..?
    By ehochedez in forum NetBeans
    Replies: 9
    Last Post: 08-27-2009, 11:32 AM
  4. how to focus to another JTextfield?
    By birdofprey in forum AWT / Swing
    Replies: 2
    Last Post: 04-09-2008, 01:08 PM
  5. Focus
    By Marty in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 02:16 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
  •