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

Discussion in 'Java' started by KevinSimonson, Nov 30, 2010.

  1. 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);
    }
    }
    KevinSimonson, Nov 30, 2010
    #1
    1. Advertising

  2. On Nov 30, 9:29 am, KevinSimonson <> wrote:
    > 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
    KevinSimonson, Nov 30, 2010
    #2
    1. Advertising

  3. In article
    <>,
    KevinSimonson <> wrote:

    > 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>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Nov 30, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    327
    Chris
    Aug 30, 2005
  2. M P
    Replies:
    1
    Views:
    435
  3. PerlFAQ Server
    Replies:
    0
    Views:
    135
    PerlFAQ Server
    Jan 24, 2011
  4. Replies:
    2
    Views:
    322
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  5. Roger
    Replies:
    3
    Views:
    298
Loading...

Share This Page