Link to home
Start Free TrialLog in
Avatar of linchong
linchong

asked on

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??
SOLUTION
Avatar of durgaprasad_j
durgaprasad_j

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of durgaprasad_j
durgaprasad_j

jp1.addKeyListener(new KeyAdapter()
      {
                 public void keyPressed(KeyEvent evt)
                 {
                         if (evt.getKeyCode() == KeyEvent.VK_F3)
                         {
                           //Do what ever you want
                        }
                  }
      });
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of linchong

ASKER

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

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

where do i put the windowlistener?
The simplest way is to

jpanel.setFocusable(true);

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

Hope this helps