Problem with KeyListener

I'm going to develop an application with a TabbedPane including some tabs; in each tab I'd like to handle different shortcuts.

Each tab contains some components (Button, TextArea, ecc) which may be focused.

I added a KeyListener on each tab but when a component is focused it retains the event and so the panel' KeyListener can't catch it..

Here is a little example without KeyListener:

import javax.swing.*;

publicclass Sampleextends JFrame

{

public Sample()

{

super.setSize(500,400);

super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel1 =new JPanel();

panel1.add(new Button("Sample Component");

JPanel panel2 =new JPanel();

panel2.add(new Button("Sample Component");

JTabbedPane pane =new JTabbedPane();

pane.addTab("Tab 1", panel1);

pane.addTab("Tab 2", panel2);

super.add(pane);

}

privatevoid F1Pressed()

{

JOptionPane.showMessageDialog(this,"F1 Pressed");

}

privatevoid F2Pressed()

{

JOptionPane.showMessageDialog(this,"F2 Pressed");

}

publicstaticvoid main(String[] args)

{

new Sample().setVisible(true);

}

}

What should I add to succed in making panel handle shortcuts?

Thank you in advance.

[2376 byte] By [afuschettoa] at [2007-11-27 9:14:10]
# 1

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Sample extends JFrame

{

public Sample()

{

super.setSize(500,400);

super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel1 = new JPanel();

panel1.add(new JButton("Sample Component"));

JPanel panel2 = new JPanel();

panel2.add(new JButton("Sample Component"));

JTabbedPane pane = new JTabbedPane();

pane.addTab("Tab 1", panel1);

pane.addTab("Tab 2", panel2);

super.add(pane);

Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener(){

public void eventDispatched(AWTEvent e)

{

if(e.getID() == KeyEvent.KEY_PRESSED)

{

if(((KeyEvent)e).getKeyCode() == KeyEvent.VK_F1) F1Pressed();

else if(((KeyEvent)e).getKeyCode() == KeyEvent.VK_F2) F2Pressed();

}

}

}, AWTEvent.KEY_EVENT_MASK);

}

private void F1Pressed()

{

JOptionPane.showMessageDialog(this, "F1 Pressed");

}

private void F2Pressed()

{

JOptionPane.showMessageDialog(this, "F2 Pressed");

}

public static void main(String[] args)

{

new Sample().setVisible(true);

}

}

Michael_Dunna at 2007-7-12 22:02:10 > top of Java-index,Desktop,Core GUI APIs...
# 2
> I added a KeyListener on each tab but when a component is focused it retains the event and so the panel' KeyListener can't catch it..You should be using "Key Bindings" http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
camickra at 2007-7-12 22:02:10 > top of Java-index,Desktop,Core GUI APIs...
# 3
you could use KeyboardFocusManageri found a example in rsb.info.nih.gov/ij/plugins/download/QT_Movie_Player.javadon磘 forget to use removeKeyEventDispatcherby
lamatiasa at 2007-7-12 22:02:10 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thank you all for your precious suggestions!
afuschettoa at 2007-7-12 22:02:11 > top of Java-index,Desktop,Core GUI APIs...