How to manipulate key events?

Hello guys,

I made a method that make a JFrame with some JLabels.

Now I want to make a method that execute some instructions when the user press the left or right key button.

I guess I need to add an actionListener in the JFrame and make an actionPerformed method. But how to identify when the left or right key are pressed?

Does someone have any tips?

[383 byte] By [Bruno_Grassellia] at [2007-11-27 9:21:52]
# 1

Your JFrame has some labels, but does it also have JButtons? I'm assuming so (left and right ?key? button?). If so, do you know how to add an actionlistener for a button? That's what you need to look up and study. Also, if you have two buttons sharing the same actionlistener, then you will need to get look at the ActionEvent object returned to find out which button was pressed. Look at the getSource() method of ActionEvent for further details.

If you are not talking about buttons but rather keyboard input, then which key? do you need to look into key bindings?You can find out about them here:

http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html

Message was edited by:

petes1234

petes1234a at 2007-7-12 22:15:58 > top of Java-index,Java Essentials,New To Java...
# 2
Hello, I have a big tip : http://forum.java.sun.com/thread.jspa?threadID=560523&tstart=75 .Hope this'll help, but if it doesn't, just say so :P
laginimaineba at 2007-7-12 22:15:58 > top of Java-index,Java Essentials,New To Java...
# 3
Look at KeyListener and KeyAdapter in the Java tutorial. You might have to resort to key binding, but this is unlikely.
Tavea at 2007-7-12 22:15:58 > top of Java-index,Java Essentials,New To Java...
# 4
> Look at KeyListener and KeyAdapter in the Java> tutorial. You might have to resort to key binding,> but this is unlikely.Key bindings are generally a better choice than KeyListeners. I would recommend using KeyBindings over KeyListeners 99 times out of 100.
Navy_Codera at 2007-7-12 22:15:58 > top of Java-index,Java Essentials,New To Java...
# 5

> Look at KeyListener and KeyAdapter in the Java

> tutorial. You might have to resort to key binding,

> but this is unlikely.

I'm going to politely disagree. KeyListeners and especially KeyAdapters are lower level constructs, and in general you want to stick with higher level constructs if possible. To quote the Java key binding tutorial:

An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to.

petes1234a at 2007-7-12 22:15:58 > top of Java-index,Java Essentials,New To Java...
# 6

> Your JFrame has some labels, but does it also have

> JButtons? I'm assuming so (left and right ?key?

> button?). If so, do you know how to add an

> actionlistener for a button? That's what you need to

> look up and study. Also, if you have two buttons

> sharing the same actionlistener, then you will need

> to get look at the ActionEvent object returned to

> find out which button was pressed. Look at the

> getSource() method of ActionEvent for further

> details.

My JFrame doesn't have JButtons. It only has three JLabels with text ("START", "OPTIONS", "QUIT").

The first Label has yellow foreground and the others has white foreground(the frame has black background).

I want to change the foregronds when the user press the right or left key (like options screen in a game, using the keyboard to select the option).

I'm reading about key bindings but I'm having problems.

Do I need to import some file to the getInputMap method work?

My code, until now:

import javax.swing.*;

import java.awt.*;

class Screens {

private JFrame window = new JFrame( "Bruno's Game" );

private Container c = window.getContentPane();

// Set Screen Settings

public void setScreenSettings () {

window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

window.setSize( 320, 240 );

c.setBackground( new Color( 0,0,0 ) );

window.setVisible( true );

}

// Options Screen

public void optionsScreen () {

String optionsString[] = { "START", "OPTIONS", "QUIT" };

JLabel options[] = new JLabel[ optionsString.length ];

for (int i = 0; i < options.length; i++) {

options[i] = new JLabel();

options[i].setForeground( new Color( 255,255,255 ) );

options[i].setSize( 300, 30 + (50 * i) );

options[i].setLocation( 10,10 );

options[i].setText( optionsString[i] );

c.add( options[i] );

}

options[0].setForeground( new Color( 255, 255, 0 ) );

InputMap inputMap = window.getInputMap();

}

}

But the javac returns the following error:

Cannot find symbol method getInputMap();

Thanks for replies.

Message was edited by:

Bruno_Grasselli

Bruno_Grassellia at 2007-7-12 22:15:58 > top of Java-index,Java Essentials,New To Java...
# 7
I found out why was the error.The JFrame doesn't suport the getInputMap method.So I needed to put the getInputMap in the first JLabel.I will try to finish the code then I'll post the results.
Bruno_Grassellia at 2007-7-12 22:15:58 > top of Java-index,Java Essentials,New To Java...
# 8
Thank you for being polite. I do appreciate that. I have to bow to your greater experience and knowledge. I have only used key bindings once (to set a default button in JTabbedPane) and so have little experience with them. I am sorry if I mislead the original poster.
Tavea at 2007-7-12 22:15:58 > top of Java-index,Java Essentials,New To Java...