Exceter
asked on
Why can't I get my GUI and KeyListener to work together?
If I do not add any of my GUI components to the JFrame's Container the KeyListener works fine. However, if I add one JButton, for example, the KeyListener no longer works. Why???
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyDemo2 extends JFrame implements KeyListener, ActionListener
{
private String line1 = "", line2 = "";
private String line3 = "";
private JTextArea textArea;
private JButton button;
public KeyDemo2()
{
super( "Demonstrating Keystroke Events" );
textArea = new JTextArea( 10, 25 );
textArea.setText( "Press any key on the keyboard..." );
textArea.setEnabled( false );
Container container = getContentPane();
container.setLayout( new FlowLayout( FlowLayout.CENTER ) );
button = new JButton("Test");
addKeyListener( this );
container.add( textArea );
container.add( button ); //If I comment out this line the Keylistener works fine.
button.addActionListener(t his);
setSize( 400, 400 );
setVisible( true );
}
public void keyPressed( KeyEvent event )
{
line1 = "Key pressed: " +
event.getKeyText( event.getKeyCode() );
setLines2and3( event );
}
public void keyReleased( KeyEvent event )
{
line1 = "Key released: " +
event.getKeyText( event.getKeyCode() );
setLines2and3( event );
}
public void keyTyped( KeyEvent event )
{
line1 = "Key typed: " + event.getKeyChar();
setLines2and3( event );
}
public void actionPerformed( ActionEvent e )
{
}
private void setLines2and3( KeyEvent event )
{
line2 = "This key is " +
( event.isActionKey() ? "" : "not " ) +
"an action key";
String temp =
event.getKeyModifiersText( event.getModifiers() );
line3 = "Modifier keys pressed: " +
( temp.equals( "" ) ? "none" : temp );
textArea.setText(
line1 + "\n" + line2 + "\n" + line3 + "\n" );
}
public static void main( String args[] )
{
KeyDemo2 application = new KeyDemo2();
application.setDefaultClos eOperation (
JFrame.EXIT_ON_CLOSE );
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyDemo2 extends JFrame implements KeyListener, ActionListener
{
private String line1 = "", line2 = "";
private String line3 = "";
private JTextArea textArea;
private JButton button;
public KeyDemo2()
{
super( "Demonstrating Keystroke Events" );
textArea = new JTextArea( 10, 25 );
textArea.setText( "Press any key on the keyboard..." );
textArea.setEnabled( false );
Container container = getContentPane();
container.setLayout( new FlowLayout( FlowLayout.CENTER ) );
button = new JButton("Test");
addKeyListener( this );
container.add( textArea );
container.add( button ); //If I comment out this line the Keylistener works fine.
button.addActionListener(t
setSize( 400, 400 );
setVisible( true );
}
public void keyPressed( KeyEvent event )
{
line1 = "Key pressed: " +
event.getKeyText( event.getKeyCode() );
setLines2and3( event );
}
public void keyReleased( KeyEvent event )
{
line1 = "Key released: " +
event.getKeyText( event.getKeyCode() );
setLines2and3( event );
}
public void keyTyped( KeyEvent event )
{
line1 = "Key typed: " + event.getKeyChar();
setLines2and3( event );
}
public void actionPerformed( ActionEvent e )
{
}
private void setLines2and3( KeyEvent event )
{
line2 = "This key is " +
( event.isActionKey() ? "" : "not " ) +
"an action key";
String temp =
event.getKeyModifiersText(
line3 = "Modifier keys pressed: " +
( temp.equals( "" ) ? "none" : temp );
textArea.setText(
line1 + "\n" + line2 + "\n" + line3 + "\n" );
}
public static void main( String args[] )
{
KeyDemo2 application = new KeyDemo2();
application.setDefaultClos
JFrame.EXIT_ON_CLOSE );
}
}
What happens when you click the mouse on the frame (not the button) and then try the keys?
ASKER
Nothing.
I assume the key listener is on the frame. Are you *sure* that the frame has the focus when you try the keys?
ASKER
I've posted the code, you can try it for yourself.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm using JDK 1.4.1.
I'm faily certain it's a focus problem because the little box that indicates the button is selected remains no matter what I do.
I'm faily certain it's a focus problem because the little box that indicates the button is selected remains no matter what I do.
ASKER
requestFocus() worked.
Thanks a million!
Thanks a million!
No problem. You might find that the following works as well:
getRootPane().setDefaultBu tton(null) ;
getRootPane().setDefaultBu
ASKER
No, that doesn't work. However, just adding requestFocus(); at the bottom of my constructor and at the bottom of my actionPerformed() method solves the problem.