I have a JLabel that has keybinded actions on it. I have defined some code for some of the Actions but, alas, there are other JLabels, a JPanel, and other things within the method (this is in main()) that I want my Actions to fool with.
I tried to change the Actions into taking parameters, but was not successful, how can I get my actions to take in parameters to manipulate? Is there any way? I have looked about but this is pretty specific and I see few good examples.
Here is a nice slab of my code:
/*Bunch of stuff I want my actions to interact with above - another JLabel, a JPanel*/
ImageIcon cursor = new ImageIcon("cursor.gif");
JLabel cursorlbl = new JLabel("", cursor, JLabel.CENTER);
Action goRight = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("lol");
}
};
Action goLeft = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("lol2");
}
};
Action goUp = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
}
};
Action goDown = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("lol2");
}
};
cursorlbl.setFocusable(true);
cursorlbl.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"),
"pressed right");
cursorlbl.getInputMap().put(KeyStroke.getKeyStroke("LEFT"),
"pressed left");
cursorlbl.getActionMap().put("pressed right", goRight);
cursorlbl.getActionMap().put("pressed left", goLeft);