Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

AddKeyListener to JOptionPane

843807Jun 1 2010 — edited Jun 1 2010
Is there a way to add a keylistener to JOptionPane.showMessageDialog?
JOptionPane.showMessageDialog(null,"press any key", "title", JOptionPane.INFORMATION_MESSAGE);
If I press enter or space I close the dialog, but i would close the dialog with any key.

Press enter--> close dialog
Press "a" -->close dialog
Press "3" -->close dialog
etcetc


I tried to extend JOptionPane
   private class MyPane extends JOptionPane implements KeyListener
   {

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		JOptionPane.showMessageDialog(null, "OKKK");
	}

	@Override
	public void keyReleased(KeyEvent e) {
		JOptionPane.showMessageDialog(null, "OKKK");
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		JOptionPane.showMessageDialog(null, "OKKK");
		
	}
   }
but no result

What is the error?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 29 2010
Added on Jun 1 2010
7 comments
1,449 views