954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is there a JOptionPane Listener?

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
}

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

bsunkel
Newbie Poster
12 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

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:

public static void main(String[] args) {
   UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
   configureOptionPane();
}
private static void configureOptionPane() {
        if(UIManager.getLookAndFeelDefaults().get("OptionPane.actionMap") == null)
        {
            UIManager.put("OptionPane.windowBindings", new 
            Object[] {
                        "ESCAPE", "close",
                        "LEFT", "left",
                        "KP_LEFT", "left",
                        "RIGHT", "right",
                        "KP_RIGHT", "right"
                    });
                    ActionMap map = new ActionMapUIResource();
                    map.put("close", new OptionPaneCloseAction());
                    map.put("left", new OptionPaneArrowAction(false));
                    map.put("right", new OptionPaneArrowAction(true));
                    UIManager.getLookAndFeelDefaults().put
            ("OptionPane.actionMap", map);
        }
    }

    private static class OptionPaneCloseAction extends AbstractAction {
        public void actionPerformed(ActionEvent e) {
            JOptionPane optionPane = (JOptionPane) e.getSource();
            optionPane.setValue(JOptionPane.CLOSED_OPTION);
        }
    }

    private static class OptionPaneArrowAction extends AbstractAction {
        private boolean myMoveRight;
        OptionPaneArrowAction(boolean moveRight) {
            myMoveRight = moveRight;
        }
        public void actionPerformed(ActionEvent e) {
            JOptionPane optionPane = (JOptionPane) e.getSource();
            EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
            
            eq.postEvent(new KeyEvent(
                    optionPane,
                    KeyEvent.KEY_PRESSED,
                    e.getWhen(),
                    (myMoveRight) ? 0 : InputEvent.SHIFT_DOWN_MASK,
                    KeyEvent.VK_TAB,
                    KeyEvent.CHAR_UNDEFINED,
                    KeyEvent.KEY_LOCATION_UNKNOWN
            ));
        }
    }
viniciusmss
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You