Why can't I get my GUI and KeyListener to work together?

Exceter
Exceter used Ask the Experts™
on
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(this);

      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.setDefaultCloseOperation(
         JFrame.EXIT_ON_CLOSE );
   }

}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
What happens when you click the mouse on the frame (not the button) and then try the keys?
Nothing.
Top Expert 2016

Commented:
I assume the key listener is on the frame. Are you *sure* that the frame has the focus when you try the keys?
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

I've posted the code, you can try it for yourself.
Top Expert 2016
Commented:
Works fine for me (Win2K, JDK 1.3). Could it be something to do with your graphics adaptor? Anyway, try requestFocus() after setVisible(true)
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.
requestFocus() worked.

            Thanks a million!
Top Expert 2016

Commented:
No problem. You might find that the following works as well:

getRootPane().setDefaultButton(null);
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial