How Do I Set the Focus so my GUI Can Read Key Events?


K

KevinSimonson

I'm trying to build a GUI that can read key events and tell which
character was keyed in. I wrote the following code to test my ability
to do that, but when I execute it, click in the <JPanel>, and type in
characters, nothing happens. It seems like I need to set the focus on
the GUI, but I don't remember precisely how to do that. Is there
anybody out there who can see what I'm doing wrong, and how I can get
the characters I enter displayed on my GUI? Thanks in advance for any
pointers you can give me.

Kevin S

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class KlBug extends JPanel implements KeyListener
{
private Font courier12;
private char[] enteredString;
private int enteredSize;

private KlBug ()
{
courier12 = new Font( "Courier", Font.PLAIN, 12);
enteredString = new char[ 1000];
enteredSize = 0;
setPreferredSize( new Dimension( 800, 600));
addKeyListener( this);
setBackground( Color.black);
}

public void paintComponent ( Graphics page)
{
super.paintComponent( page);
page.setFont( courier12);
page.setColor( Color.green);
int xLctn = 60;
int yLctn = 60;
for (int ec = 0; ec < enteredSize; ec++)
{ if (enteredString[ ec] != '\n')
{ page.drawString( "" + enteredString[ ec], xLctn, yLctn + 10);
xLctn += 7;
}
else
{ yLctn += 12;
xLctn = 60;
}
}
}

public void keyPressed ( KeyEvent evnt) {}
public void keyReleased ( KeyEvent evnt) {}

public void keyTyped ( KeyEvent evnt)
{
System.out.println( "Key '" + evnt.getKeyChar() + "' typed.");
enteredString[ enteredSize++] = evnt.getKeyChar();
repaint();
}

public static void main ( String[] arguments)
{
KlBug kb = new KlBug();
JFrame kbFrame = new JFrame( "KlBug");
kbFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
kbFrame.getContentPane().add( kb);
kbFrame.pack();
kbFrame.setVisible( true);
}
}
 
Ad

Advertisements

K

KevinSimonson

I'm trying to build a GUI that can read key events and tell which
character was keyed in.  I wrote the following code to test my ability
to do that, but when I execute it, click in the <JPanel>, and type in
characters, nothing happens.  It seems like I need to set the focus on
the GUI, but I don't remember precisely how to do that.  Is there
anybody out there who can see what I'm doing wrong, and how I can get
the characters I enter displayed on my GUI?  Thanks in advance for any
pointers you can give me.

Kevin S

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class KlBug extends JPanel implements KeyListener
{
  private   Font courier12;
  private char[] enteredString;
  private    int enteredSize;

  private KlBug ()
  {
    courier12     = new Font( "Courier", Font.PLAIN, 12);
    enteredString = new char[ 1000];
    enteredSize   = 0;
    setPreferredSize( new Dimension( 800, 600));
    addKeyListener( this);
    setBackground( Color.black);
  }

  public void paintComponent ( Graphics page)
  {
    super.paintComponent( page);
    page.setFont( courier12);
    page.setColor( Color.green);
    int xLctn = 60;
    int yLctn = 60;
    for (int ec = 0; ec < enteredSize; ec++)
    { if (enteredString[ ec] != '\n')
      { page.drawString( "" + enteredString[ ec], xLctn, yLctn + 10);
        xLctn += 7;
      }
      else
      { yLctn += 12;
        xLctn  = 60;
      }
    }
  }

  public void keyPressed  ( KeyEvent evnt) {}
  public void keyReleased ( KeyEvent evnt) {}

  public void keyTyped    ( KeyEvent evnt)
  {
    System.out.println( "Key '" + evnt.getKeyChar() + "' typed.");
    enteredString[ enteredSize++] = evnt.getKeyChar();
    repaint();
  }

  public static void main ( String[] arguments)
  {
    KlBug kb       = new KlBug();
    JFrame kbFrame = new JFrame( "KlBug");
    kbFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    kbFrame.getContentPane().add( kb);
    kbFrame.pack();
    kbFrame.setVisible( true);
  }

}

Never mind. The answer was to call <setFocusable( true)>.

Kevin S
 
Ad

Advertisements

J

John B. Matthews

KevinSimonson said:
I'm trying to build a GUI that can read key events and tell which
character was keyed in. I wrote the following code to test my
ability to do that, but when I execute it, click in the <JPanel>, and
type in characters, nothing happens. It seems like I need to set the
focus on the GUI, but I don't remember precisely how to do that. Is
there anybody out there who can see what I'm doing wrong, and how I
can get the characters I enter displayed on my GUI? Thanks in
advance for any pointers you can give me.

Try panel.setFocusable(true), as mentioned here:

<http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html>

Also, consider whether you should be using key bindings, instead:

<http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top