KeyListener, Keyevent, Cursor keys

Hi,

I am new to Java as well as new to Event Handling. I am building a GUI using Netbeans 5 and would like to utilize the four cursor keys in my application. For now I just would like to show a dialog box if one og the cursor keys has been pressed. However, my code doesn't interpret the cursor keys (I just tried it with the cursor key left = VK_LEFT):

import java.awt.event.KeyEvent;

import javax.swing.JOptionPane;

publicclass Hauptfensterextends javax.swing.JFrame{

/** Creates new form Hauptfenster */

public Hauptfenster(){

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

privatevoid initComponents(){

jPanel1 =new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setTitle("Test");

setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

jPanel1.setBackground(new java.awt.Color(51, 102, 255));

jPanel1.setPreferredSize(new java.awt.Dimension(400, 400));

jPanel1.addKeyListener(new java.awt.event.KeyAdapter(){

publicvoid keyPressed(java.awt.event.KeyEvent evt){

CursorHinauf(evt);

CursorHinunter(evt);

CursorRechts(evt);

CursorLinks(evt);

}

});

org.jdesktop.layout.GroupLayout jPanel1Layout =new org.jdesktop.layout.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(0, 800, Short.MAX_VALUE)

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(0, 800, Short.MAX_VALUE)

);

org.jdesktop.layout.GroupLayout layout =new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)

);

pack();

}// </editor-fold>

privatevoid CursorLinks(java.awt.event.KeyEvent evt){

// TODO add your handling code here:

if(evt.getKeyCode() == KeyEvent.VK_LEFT){

JOptionPane.showMessageDialog(jPanel1,"Left");

}

}

privatevoid CursorRechts(java.awt.event.KeyEvent evt){

// TODO add your handling code here:

}

privatevoid CursorHinunter(java.awt.event.KeyEvent evt){

// TODO add your handling code here:

}

privatevoid CursorHinauf(java.awt.event.KeyEvent evt){

// TODO add your handling code here:

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new Hauptfenster().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JPanel jPanel1;

// End of variables declaration

}

Thanks for your help!

[5810 byte] By [SFLa] at [2007-10-2 15:17:16]
# 1

Hi,

you can't get any KeyEvent while your Component, the jPanel1 is not focusable. Use the method:

setFocusable(true)

to achieve that.

Look at the page: http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html for more information about the focus system.

Stephan

stephanXa at 2007-7-13 14:21:34 > top of Java-index,Security,Event Handling...
# 2
Guessen sie sich.When I built a Tetris game I used the KeyboardFocusManager to get cursor presses. It was very effective so check it out if your interested.
tjacobs01a at 2007-7-13 14:21:34 > top of Java-index,Security,Event Handling...
# 3
I checked the "focusable" checkbox in Netbeans for my JPanel1, however, it seems that Netbeans doesn't insert it into the source code.I now get my KeyEvents from my JFrame and not from the JPanel - it works great :) Is this ok or should I better get the events from my JPanel1?
SFLa at 2007-7-13 14:21:34 > top of Java-index,Security,Event Handling...
# 4

> I checked the "focusable" checkbox in Netbeans for my

> JPanel1, however, it seems that Netbeans doesn't

> insert it into the source code.

Thats strange. Anyway, the article about focushandling mentions an other way to make your component focusable. Use the method

JComponent.getInputMap(JComponent.WHEN_FOCUSED).put(...)

instead of setFocusable(). Then your jpanel1 should be focusable.

> I now get my KeyEvents from my JFrame and not from

> the JPanel - it works great :) Is this ok or should

> I better get the events from my JPanel1?

I would register the listener at the JPanel1, because this component encapsulate a set of functionality and I suppose your code won't work when the frame contains more components.

Create your own component class by subclassing JComponent. Inside this class write your code without referencing the JFrame, which contains your component.

Stephan

stephanXa at 2007-7-13 14:21:34 > top of Java-index,Security,Event Handling...
# 5

Special keys, like the arrow or functionkeys, are caught within the keyPressed() handler rather than through keyTyped(). To identify the keys, you use their virtual key codes.

For example:

myTextfield.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();

if(key == KeyEvent.VK_LEFT) System.out.println("LEFT ARROW");

if(key == KeyEvent.VK_RIGHT) System.out.println("RIGHT ARROW");

if(key == KeyEvent.VK_UP) System.out.println("UP ARROW");

if(key == KeyEvent.VK_DOWN) System.out.println("DOWN ARROW");

}

arowberrya at 2007-7-13 14:21:34 > top of Java-index,Security,Event Handling...