Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

where do the keystrokes go?

Thread view: 
Martijn Mulder - 18 Dec 2005 19:37 GMT
I try to attach a KeyListener to my JComponent but no
keystrokes make it to the JComponent. MouseMotions, on
the other hand, are percieved by the JComponent. I do not
understand where the keystrokes go, since both Listeneres
are attached in identical ways. This code illustrates the
problem. When the mouse is moved over the JFrame,
a long stream of 'mouseMoved()' messages is send to
stdout. Pressing keys when the JFrame has focus yields
no output.

//class MouseAndKeys
class MouseAndKeys extends javax.swing.JComponent
{

//constructor
MouseAndKeys()
{
 super();
 addMouseMotionListener(new javax.swing.event.MouseInputAdapter()
 {
  public void mouseMoved(java.awt.event.MouseEvent a)
  {
   System.out.println("mouseMoved()");
  }
 });
 addKeyListener(new KeyAdapter()
 {
  public void keyPressed(java.awt.event.KeyEvent a)
  {
   System.out.println("keyPressed()");
  }
 });
}

//main
public static void main(String[]a)
{
 javax.swing.JFrame jframe=new javax.swing.JFrame("MouseAndKeys");
 jframe.getContentPane().add(new MouseAndKeys());
 jframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
 jframe.pack();
 jframe.setSize(610,377);
 jframe.setVisible(true);
}
}
Chris Smith - 18 Dec 2005 19:46 GMT
> I try to attach a KeyListener to my JComponent but no
> keystrokes make it to the JComponent. MouseMotions, on
[quoted text clipped - 5 lines]
> stdout. Pressing keys when the JFrame has focus yields
> no output.

Well, for one thing, your component needs focus.  The containing frame
having focus isn't good enough.  (Can you imagine if every time you
pressed a key while a dialog had focus, EVERY textfield in that dialog
received the key event?)  To give focus to your new control, you'll
first need to call setFocusable(true).

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Martijn Mulder - 18 Dec 2005 19:54 GMT
:-) Thank you, Chris. Hours of thumbing through books
led nowhere, posting did. Here is the fixed code. Moving
the mouse over the JFrame or pressing keys when JFrame
has focus makes the JComponent respond:

//class MouseAndKeys
class MouseAndKeys extends javax.swing.JComponent
{

//constructor
MouseAndKeys()
{
 super();
 setFocusable(true);
 addMouseMotionListener(new javax.swing.event.MouseInputAdapter()
 {
  public void mouseMoved(java.awt.event.MouseEvent a)
  {
   System.out.println("mouseMoved()");
  }
 });
 addKeyListener(new KeyAdapter()
 {
  public void keyPressed(java.awt.event.KeyEvent a)
  {
   System.out.println("keyPressed()");
  }
 });
}

//main
public static void main(String[]a)
{
 javax.swing.JFrame jframe=new javax.swing.JFrame("MouseAndKeys");
 jframe.getContentPane().add(new MouseAndKeys());
 jframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
 jframe.pack();
 jframe.setSize(610,377);
 jframe.setVisible(true);
}
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.