In the following program, i'm trying to change the text of a label based on the key pressed, but i dont know how to do it. The statements to be executed when a key is pressed are defined in actionPerformed() method of the TimerListener InnerClass. but I dont understand how do I access the label from there.
package aircraftPackage;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Controller extends JPanel {
private static final long serialVersionUID = 1L;
public static final int STEP = 3;
private static final int TIMER_DELAY = STEP * 8;
private BufferedImage playerImage = null;
private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>();
enum Direction {
UP(KeyEvent.VK_UP, 0, -1), DOWN(KeyEvent.VK_DOWN, 0, 1),
LEFT(KeyEvent.VK_LEFT, -1, 0), RIGHT(KeyEvent.VK_RIGHT, 1, 0);
private int keyCode;
private int xDirection;
private int yDirection;
private Direction(int keyCode, int xDirection, int yDirection) {
this.keyCode = keyCode;
this.xDirection = xDirection;
this.yDirection = yDirection;
}
public int getKeyCode() {
return keyCode;
}
public int getXDirection() {
return xDirection;
}
public int getYDirection() {
return yDirection;
}
}
public Controller() {
for (Direction direction : Direction.values()) {
directionMap.put(direction, false);
}
setKeyBindings();
Timer timer = new Timer(TIMER_DELAY, new TimerListener());
timer.start();
}
private void setKeyBindings() {
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actMap = getActionMap();
for (final Direction direction : Direction.values()) {
KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false);
KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true);
inMap.put(pressed, direction.toString() + "pressed");
inMap.put(released, direction.toString() + "released");
actMap.put(direction.toString() + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
directionMap.put(direction, true);
}
});
actMap.put(direction.toString() + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
directionMap.put(direction, false);
}
});
}
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
boolean moved = false;
for (Direction direction : Direction.values()) {
if (directionMap.get(direction)) {
if (direction.keyCode == 37) {
System.out.println("go LEFT");
} else if (direction.getKeyCode() == 39) {
System.out.println("go RIGHT");
} else if (direction.getKeyCode() == 38) {
System.out.println("go UP");
}
else if (direction.getKeyCode()==40){
System.out.println("go DOWN");
}
}
}
}
}
public static void createAndShowUI() {
JFrame frame = new JFrame("MoveIcon");
JPanel panel = new JPanel();
JLabel jl = new JLabel();
jl.setText("testing....");
frame.add(jl);
frame.add(panel);
frame.getContentPane().add(new Controller());
new Controller();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
@SuppressWarnings("static-access")
public void run() {
createAndShowUI();
}
});
}
}
Actually, what I wanted to do was, to create a nice form which would contain various labels in Netbeans(its easier to do it there) and use the above code just to know which key has been pressed and change values of each label accordingly. I tried it, but it didnt work. Please help me on this. Thanks.
PS : You'll find the parts of above code in some questions or on some sites, Because I didnt write it all by myself. I dont have any experience with keybindings/keyListeners etc before. this is just a part of a project im doing.
I tried to remove everything that you don't need right now, and added a JLabel
which displays the directions like this:
This should get you started.
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class Controller extends JPanel {
private static final long serialVersionUID = 1L;
private static final int STEP = 3;
private static final int TIMER_DELAY = STEP * 8;
private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>();
private JLabel lblDirection = new JLabel();
enum Direction {
UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN),
LEFT(KeyEvent.VK_LEFT), RIGHT(KeyEvent.VK_RIGHT);
private int keyCode;
private Direction(int keyCode) {
this.keyCode = keyCode;
}
public int getKeyCode() {
return keyCode;
}
}
public Controller() {
add(lblDirection);
for (Direction direction : Direction.values()) {
directionMap.put(direction, false);
}
setKeyBindings();
Timer timer = new Timer(TIMER_DELAY, new TimerListener());
timer.start();
}
private void setKeyBindings() {
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actMap = getActionMap();
for (final Direction direction : Direction.values()) {
KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false);
KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true);
inMap.put(pressed, direction.toString() + "pressed");
inMap.put(released, direction.toString() + "released");
actMap.put(direction.toString() + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
directionMap.put(direction, true);
}
});
actMap.put(direction.toString() + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
directionMap.put(direction, false);
}
});
}
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
StringBuilder s = new StringBuilder("go ");
for (Direction direction : Direction.values()) {
if (directionMap.get(direction)) {
s.append(direction.name() + " ");
}
}
lblDirection.setText(s.toString());
}
}
public static void createAndShowUI() {
JFrame frame = new JFrame("KeyMapping");
frame.getContentPane().add(new Controller());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowUI();
}
});
}
}