2

I searched for answers, but all i found is workaounds, not the reason, so i am asking this question:

I am new to GUI programming. While practicing some code regarding key event handling, i came accross an example that has a JTextArea contained inside a JFrame. The keylistener interface is implemented by the Frame itself. When a key is pressed some relevant information are shown on the text area based on the key pressed. the code works fine.

but i tried to go differently and tried to register the JTextarea to a keyListenr instead of the JFrame. However, this does not respond to key events. Here is the code below. Please help.

public class KeyDemoFrame extends JFrame
{
private String line1 = "";
private String line2 = "";
private String line3 = "";
private JTextArea textArea;

public KeyDemoFrame()
{
    super("Demonstrating Keystrong events");
    textArea = new JTextArea(10,21);
    textArea.setText("Press any key on keyboard");
    textArea.setEnabled(false);
    textArea.setDisabledTextColor(Color.BLACK);
    add(textArea);
    //addKeyListener( this );

    KeyEventHandler keyStrokeHandler = new KeyEventHandler();
    addKeyListener(keyStrokeHandler);
}   
    private class KeyEventHandler implements KeyListener
    {
        public void keyPressed(KeyEvent event)
        {
            line1 = String.format("Your pressed the %s key", KeyEvent.getKeyText(event.getKeyCode()));
            setLines2and3(event);

        }
        public void keyReleased(KeyEvent event)
        {
            line1 = String.format("You released %s key", KeyEvent.getKeyText(event.getKeyCode()));
            setLines2and3(event);
        }
        public void keyTyped(KeyEvent event)
        {
            line1 = String.format("You typed %s key",KeyEvent.getKeyText(event.getKeyCode()));
            setLines2and3(event);
        }
        private void setLines2and3(KeyEvent event)
        {
            line2 = String.format("This key is %san action key", (event.isActionKey()?"":"not "));
            String temp = KeyEvent.getKeyModifiersText( event.getModifiers() );
            line3 = String.format( "Modifier keys pressed: %s",( temp.equals( "" ) ? "none" : temp ) );
            textArea.setText( String.format( "%s\n%s\n%s\n",line1, line2, line3 ) );
        }
    }

}

import javax.swing.JFrame;

public class KeyDemo
{
public static void main(String[] args)
{
    KeyDemoFrame keyDemoFrame = new KeyDemoFrame();
    keyDemoFrame.setSize(300, 100);
    keyDemoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    keyDemoFrame.setVisible(true);
}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
yeasir
  • 23
  • 2
  • To be more precise, as to why is this happening, is because, you are extending `JFrame` , and you are adding `Listener` to the Frame itself instead of adding it to the `JTextArea` as desired by you. And as adviced by @mKorbel , you should be using `DocumentListener` for `JTextComponent` :-) – nIcE cOw May 14 '12 at 16:56

2 Answers2

2
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

The reason for this behavior is that the JTextComponent handles the KeyEvent. As mKorbel already indicated you should use a DocumentListener for JTextComponents

Robin
  • 36,233
  • 5
  • 47
  • 99