Keyboard listener don't work!

The code:

private void this_keyTyped(KeyEvent e)

{

if(e.getKeyCode()==e.VK_ENTER) // this is just a test

{

jLabel1.setLocation(new Point(x--, y));

}

else

{

System.out.println("\n Error!");

}

}

do not work!

It moves when:

private void this_keyTyped(KeyEvent e)

{

jLabel1.setLocation(new Point(x--, y));

}

What is wrong?

Whole code:

/****************************************************************/

/* av*/

/* */

/****************************************************************/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* Summary description for av

*

*/

public class av extends JFrame

{

// Variables declaration

int x;

int y;

private JLabel jLabel1;

private JPanel contentPane;

// End of variables declaration

public av()

{

super();

initializeComponent();

//

// TODO: Add any constructor code after initializeComponent call

//

this.setVisible(true);

}

/**

* 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 Windows Form Designer. Otherwise, retrieving design might not work properly.

* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder

* to retrieve your design properly in future, before revising this method.

*/

private void initializeComponent()

{

jLabel1 = new JLabel();

contentPane = (JPanel)this.getContentPane();

x = 164;

y = 106;

//

// jLabel1

//

jLabel1.setIcon(new ImageIcon("D:\\Documents and Settings\\Administrator\\Desktop\\b-batsu.GIF"));

//

// contentPane

//

contentPane.setLayout(null);

contentPane.setBackground(new Color(255, 254, 254));

addComponent(contentPane, jLabel1, 164,106,30,42);

//

// av

//

this.setTitle("av - extends JFrame");

this.setLocation(new Point(30, 37));

this.setSize(new Dimension(390, 300));

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

this.addKeyListener(new KeyAdapter() {

public void keyTyped(KeyEvent e)

{

this_keyTyped(e);

}

});

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

private void addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

//

// TODO: Add any appropriate code in the following Event Handling Methods

//

private void this_keyTyped(KeyEvent e)

{

if(e.getKeyCode()==e.VK_ENTER) // this is just a test

{

jLabel1.setLocation(new Point(x--, y));

}

else

{

System.out.println("\n Error!");

}

}

//

// TODO: Add any method code to meet your needs in the following area

//

//============================= Testing ================================//

//==//

//= The following main method is just for testing this class you built.=//

//= After testing,you may simply delete it.=//

//======================================================================//

public static void main(String[] args)

{

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

try

{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}

catch (Exception ex)

{

System.out.println("Failed loading L&F: ");

System.out.println(ex);

}

new av();

}

//= End of Testing =

}

[4114 byte] By [hannesruns@hotmail.coma] at [2007-10-2 11:28:38]
# 1

Probably shouldn't've posted this in the Game Development forum, but I might as well answer this any way.

First of all, VK_E should be referenced staticly through KeyEvent.VK_E, not e.VK_E

Next, you need to learn that in order for any kind of event to register, the registered Component has to have focus. In other words, in order for your class to pick up key events, that has to have focus. What is focus? Read some tutorials; in short, an example is what happens when you click on any button in any program; a little dotted line runs around the button, telling you that it has focus. If you click on a frame, the frame then has focus.

To make a component grab focus, use the component.grabFocus() method.

To see if a component has the focus, use the component.hasFocus() method.

Try calling this.grabFocus() after all of the components are initialized in your program; what the problem might be is that the JLabel has the focus and not the frame, so the key events are not getting registered with the KeyAdapter you added.

If you have already tried this and it still did not work, sorry for breaking the peace =D

Alex

aRyan316a at 2007-7-13 4:47:45 > top of Java-index,Other Topics,Java Game Development...
# 2
hi there :) why don't you try this:if (e.getKeyCode() == KeyEvent.VK_ENTER) {//blah}that should do the trick :)
JohnBakkera at 2007-7-13 4:47:45 > top of Java-index,Other Topics,Java Game Development...