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??
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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??
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.
in that case , you might have to use reuqestfocus as ashoksep said.
ASKER
I am using these
JDesktopPane desktop = null;
private JInternalFrame jif = null;
JPanel p=new JPanel();
where do i put the windowlistener?
JDesktopPane desktop = null;
private JInternalFrame jif = null;
JPanel p=new JPanel();
where do i put the windowlistener?
ASKER
The simplest way is to
jpanel.setFocusable(true);
and runs well in any container.
jpanel.setFocusable(true);
and runs well in any container.
ASKER
too bad, on closer trial, it is not very reliable.
when my cursor is in the jtextfield, it does not work anymore.
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
addKeyListener to jtextfield , when you get F3 in that, you execute JpanelObject.grabFocus() .
Hope this helps
{
public void keyPressed(KeyEvent evt)
{
if (evt.getKeyCode() == KeyEvent.VK_F3)
{
//Do what ever you want
}
}
});