944,120 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5691
  • Java RSS
Sep 6th, 2005
0

Is there a JOptionPane Listener?

Expand Post »
Hi all,

I have a JOptionPane with a YES button and a NO button. All I want to do is be able to press the left or right button on the keyboard to change the focus on the buttons. By using the mouse it works. Is there some kind of Listener I can use?

Here is my code :

====================================================

int response = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit","Exit?",

JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE);

if(response == JOptionPane.YES_OPTION){
System.exit(1);
}else {
//do nothing
}

=====================================================
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bsunkel is offline Offline
12 posts
since Nov 2004
Sep 6th, 2005
0

Re: Is there a JOptionPane Listener?

You could TRY a keylistener but I'm not sure that'll work out exactly like you want. There should already be a mechanism built in that changes components when tab is pressed. I would stick to that if I were you.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Sep 7th, 2005
0

Re: Is there a JOptionPane Listener?

From Windows, usually hitting TAB will change focus between components. If you must use the arrow keys, you could add a KeyListener which changes the focus to the button components. I would probably make a new class and extend the JOptionPane
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jun 7th, 2010
0
Re: Is there a JOptionPane Listener?
Copy and paste the code below to your Main class.
So call configureOptionPane() at your main method.
Also use UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE) to make enter follows focus.
Example:
Java Syntax (Toggle Plain Text)
  1. public static void main(String[] args) {
  2. UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
  3. configureOptionPane();
  4. }
Java Syntax (Toggle Plain Text)
  1. private static void configureOptionPane() {
  2. if(UIManager.getLookAndFeelDefaults().get("OptionPane.actionMap") == null)
  3. {
  4. UIManager.put("OptionPane.windowBindings", new
  5. Object[] {
  6. "ESCAPE", "close",
  7. "LEFT", "left",
  8. "KP_LEFT", "left",
  9. "RIGHT", "right",
  10. "KP_RIGHT", "right"
  11. });
  12. ActionMap map = new ActionMapUIResource();
  13. map.put("close", new OptionPaneCloseAction());
  14. map.put("left", new OptionPaneArrowAction(false));
  15. map.put("right", new OptionPaneArrowAction(true));
  16. UIManager.getLookAndFeelDefaults().put
  17. ("OptionPane.actionMap", map);
  18. }
  19. }
  20.  
  21. private static class OptionPaneCloseAction extends AbstractAction {
  22. public void actionPerformed(ActionEvent e) {
  23. JOptionPane optionPane = (JOptionPane) e.getSource();
  24. optionPane.setValue(JOptionPane.CLOSED_OPTION);
  25. }
  26. }
  27.  
  28. private static class OptionPaneArrowAction extends AbstractAction {
  29. private boolean myMoveRight;
  30. OptionPaneArrowAction(boolean moveRight) {
  31. myMoveRight = moveRight;
  32. }
  33. public void actionPerformed(ActionEvent e) {
  34. JOptionPane optionPane = (JOptionPane) e.getSource();
  35. EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
  36.  
  37. eq.postEvent(new KeyEvent(
  38. optionPane,
  39. KeyEvent.KEY_PRESSED,
  40. e.getWhen(),
  41. (myMoveRight) ? 0 : InputEvent.SHIFT_DOWN_MASK,
  42. KeyEvent.VK_TAB,
  43. KeyEvent.CHAR_UNDEFINED,
  44. KeyEvent.KEY_LOCATION_UNKNOWN
  45. ));
  46. }
  47. }
Last edited by viniciusmss; Jun 7th, 2010 at 11:20 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viniciusmss is offline Offline
1 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Find selected shapes
Next Thread in Java Forum Timeline: Question about inheritance





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC