Results 1 to 2 of 2
- 05-11-2011, 08:45 PM #1
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
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 09:07 PM.
- 05-11-2011, 09:12 PM #2
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
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: http://www.java-forums.org/new-java/...y-pressed.html
Similar Threads
-
Button without focus
By comeofage in forum SWT / JFaceReplies: 0Last Post: 04-27-2010, 09:45 AM -
Focus scroll bar
By BlackTea in forum SWT / JFaceReplies: 0Last Post: 02-25-2010, 07:16 PM -
How to avoid focus()..?
By ehochedez in forum NetBeansReplies: 9Last Post: 08-27-2009, 12:32 PM -
how to focus to another JTextfield?
By birdofprey in forum AWT / SwingReplies: 2Last Post: 04-09-2008, 02:08 PM -
Focus
By Marty in forum AWT / SwingReplies: 1Last Post: 05-31-2007, 03:16 AM
Bookmarks