?
Solved

enable a keystroke for a JPanel

Posted on 2005-03-17
9
Medium Priority
?
1,161 Views
Last Modified: 2008-01-09
Hi

I have a few fields within a container.

I wish to add keylistener to the whole JPanel. for example, when i press F3, I wish to send the content to a print class. How can I do it??
0
Comment
Question by:linchong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 6

Assisted Solution

by:durgaprasad_j
durgaprasad_j earned 100 total points
ID: 13572122
hi,
suppose if Jpanel is jp1, then
try using jp1.addKeyListener( ... )
0
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13572130
jp1.addKeyListener(new KeyAdapter()
      {
                 public void keyPressed(KeyEvent evt)
                 {
                         if (evt.getKeyCode() == KeyEvent.VK_F3)
                         {
                           //Do what ever you want
                        }
                  }
      });
0
 
LVL 5

Accepted Solution

by:
ashok3sep earned 100 total points
ID: 13572785
You can find some examples about the use of KeyListeners

----> http://www.javaalmanac.com/egs/javax.swing/KeyStrokes.html


---->You could add a key Listener to your component as Durgaprasad said..

If you need to see what the user has pressed then....

YourComponenet.addKeyListener(new KeyAdapter()
{
  public void keyTyped(KeyEvent e)
  {
   //This should display any key typed
  // on dos mode . . .
   System.out.println(e.getKeyCode());
  }
});

you need to call requestFocus() method of the Panel like in the below method...

if your panel doersnt respnd to the KeyListener then add to frame/window and see what happens......
like this

class Keys extends KeyAdapter{
     public void keyPressed(KeyEvent e) {
          System.out.println(e);
     }
}

class Window1 extends JFrame implements WindowFocusListener{
         JPanel pad;
     public Window1() {
          super("Window");
                 setSize(300,400);
          this.addWindowFocusListener(this);
          pad = new JPanel();
          pad.addKeyListener(new Keys());
          this.validate();
          this.show();
     }
     
     public void windowGainedFocus(WindowEvent e) {
          pad.requestFocus();

     }
         public void windowLostFocus(WindowEvent e) {}

}


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:linchong
ID: 13573030
I know how to add a keylistener to a jtextfield etc ....

the jpanel ontains a few jtextfield. when i add a keylistener to the jpanel, it does not respond at all cos the cursor is in a jtextfield.

it does not work. how can i solve this??
0
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13573147
hi,
 in that case , you might have to use reuqestfocus as ashoksep said.
0
 

Author Comment

by:linchong
ID: 13589228
I am using these

JDesktopPane desktop = null;
private JInternalFrame jif = null;
JPanel p=new JPanel();

where do i put the windowlistener?
0
 

Author Comment

by:linchong
ID: 13589659
The simplest way is to

jpanel.setFocusable(true);

and runs well in any container.
0
 

Author Comment

by:linchong
ID: 13599517
too bad, on closer trial, it is not very reliable.
when my cursor is in the jtextfield, it does not work anymore.
0
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13602758
hi,
  addKeyListener to jtextfield , when you get F3 in that, you execute JpanelObject.grabFocus() .

Hope this helps
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question