• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1172
  • Last Modified:

enable a keystroke for a JPanel

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
linchong
Asked:
linchong
  • 4
  • 4
2 Solutions
 
durgaprasad_jCommented:
hi,
suppose if Jpanel is jp1, then
try using jp1.addKeyListener( ... )
0
 
durgaprasad_jCommented:
jp1.addKeyListener(new KeyAdapter()
      {
                 public void keyPressed(KeyEvent evt)
                 {
                         if (evt.getKeyCode() == KeyEvent.VK_F3)
                         {
                           //Do what ever you want
                        }
                  }
      });
0
 
ashok3sepCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
linchongAuthor Commented:
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
 
durgaprasad_jCommented:
hi,
 in that case , you might have to use reuqestfocus as ashoksep said.
0
 
linchongAuthor Commented:
I am using these

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

where do i put the windowlistener?
0
 
linchongAuthor Commented:
The simplest way is to

jpanel.setFocusable(true);

and runs well in any container.
0
 
linchongAuthor Commented:
too bad, on closer trial, it is not very reliable.
when my cursor is in the jtextfield, it does not work anymore.
0
 
durgaprasad_jCommented:
hi,
  addKeyListener to jtextfield , when you get F3 in that, you execute JpanelObject.grabFocus() .

Hope this helps
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now