KeyStroke Problem...

Hi

I added KeyStroke to mainPanel to look for Enter key pressed using InputMap and ActionMap. The requirement is whereever the focus is pressing enter next button should be activated.

It works okay when no dialog is used inside the panel. But when a JOptionPane or JDialog is created hitting enter on dialog buttons, the keystroke is also passed to the panel. Which is not allowing the dialog to close. Do any one have solution for this?

Im aware of the optional way to add KeyListener to all the components in the mainPanel which i dont want to do...

I have attached the sample code....

*******************************************************

public class KeyStroke extends JFrame implements ActionListener {

private JButton next;

public KeyStroke(){

JPanel mainPanel=new JPanel();

JTextField textarea=new JTextField(15);

next=new JButton("Next");

next.addActionListener(this);

mainPanel.add(textarea);

mainPanel.add(next);

Action pressedEnterKeyAction = new AbstractAction() {

public void actionPerformed(ActionEvent arg0) {

next.doClick();

}

};

mainPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ).

put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,true),"pressedEnter");

mainPanel.getActionMap().put("pressedEnter",pressedEnterKeyAction);

this.getContentPane().add(mainPanel);

}

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(this,"Button Clicked", "dialog",

JOptionPane.INFORMATION_MESSAGE);

}

*************************************************

[1676 byte] By [kaannamaa] at [2007-10-2 5:26:22]
# 1
1) Use the "code" tags when posting code so it retains its original formatting.2) Use a better name for your test class. KeyStroke is a class in the JDK3) The code you posted isn't compilable or executable.
camickra at 2007-7-16 1:28:09 > top of Java-index,Desktop,Core GUI APIs...