Reputation: 1
I need some help on my java code I'm a newbie in java, What I want to do is once I press a letter I want to display some message just to verify that the code works. Using javax swing library specifically the keylistener and key event and I don't know if I'm doing it right. here are my code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class KeyStrokeButton{
public JFrame frame;
public JPanel panel;
public JButton btnfocus, btnrelease, btnwindow, btnancestor;
public JTextField text;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
KeyStrokeButton mydesign = new KeyStrokeButton();
mydesign.design();
}
});
}
public void design()
{
panel = new JPanel();
panel.setLayout(null);
frame = new JFrame("Keystroke Tutorials");
frame.getContentPane().add(panel);
frame.setSize(250, 250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnfocus = new JButton("<html><center>Focus");
btnfocus.setBounds(10, 10, 90, 80);
panel.add(btnfocus);
btnfocus.addKeyListener(new ClickPressed());
btnrelease = new JButton("Release");
btnrelease.setBounds(110, 10, 90, 80);
panel.add(btnrelease);
btnancestor = new JButton("Ancestor");
btnancestor.setBounds(10, 100, 90, 80);
panel.add(btnancestor);
btnwindow = new JButton("Window");
btnwindow.setBounds(110, 100, 90, 80);
panel.add(btnwindow);
}
class ClickPressed implements KeyListener
{
public void KeyPressed(KeyEvent e)
{
//any code.
}
}
}
as much as possible I want to separate my code for the event, so I create a class to make it readable.
Upvotes: -3
Views: 9364
Reputation: 23
Try the following code to build a KeyListener within a JFrame: the code is set up so that the enter and escape keys are detected when pressed.
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
class KeyListener extends JFrame implements KeyListener {
@Override
public void keyReleased(KeyEvent e) { // Required for KeyListener override
}
@Override
public void keyTyped(KeyEvent e) { // Required for KeyListener override
}
}
public KeyListener() {
addKeyListener(this); // KeyListener added to JFrame to detect key strokes
setFocusable(true); // Ensure JFrame can gain focus
}
public void keyPressed(KeyEvent e) { // Code to handle when key pressed
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_ENTER: // Code to handle when enter key pressed
// ENTER CODE HERE
break;
case KeyEvent.VK_ESCAPE: // Code to handle when Escape key pressed
// ENTER CODE HERE
break;
} }
Upvotes: 0
Reputation: 6307
Sujay's answer will give you all the info you need, but here is a quick example of listening for key events.
You can add a global key listener to your JFrame by adding this after launch:
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher (new MyDispatcher());
You then need to create a key event dispatcher, and add your code into it
private class MyDispatcher implements KeyEventDispatcher
{
@Override
public boolean dispatchKeyEvent(KeyEvent e)
{
if (e.getKeyCode() == 38) //up key
{
//Do something when the up key is pressed
System.out.println("The up key was pressed");
}
else if (e.getKeyCode() == 40) //down key
{
//Do something when the down key is pressed
System.out.println("The down key was pressed");
}
return false;
}
}
Upvotes: 4
Reputation: 347334
KeyListener
in this context is based on the current focus. That is, key events will only be delivered to the component that current has focus.
So, for example, you have 3 buttons. The focus button will only ever receive a key event when it has focus. If any other component has focus, the focus button will not receive key events.
Generally speaking, KeyListener
s are not a good idea. You're much better using the Key Bindings API
A better explanation of what you are trying to achieve would help us further.
Upvotes: 6
Reputation: 6783
I don't know whether there's a real question lurking anywhere in what you've posted. But judging from your code, here're a couple of pointers:
Setting a null layout is never a good idea. As a basic step learn about layouts. So read about setting layouts over here: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
You also need to learn how to work with KeyListeners. Here's another link that might help you with this: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
I hope this will help you get started.
Upvotes: 4