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.
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");
}
}
}