JOptionPane.YES_OPTION == ENTER on each button

爷,独闯天下 提交于 2019-12-05 12:26:12

It all depends on the look 'n feel, AFAIK. In your L&F, "Enter" means "press the default button" (which is Yes). Pressing the focused button is probably done by pressing the space bar.

This will make the uimanager handle the focused button. This will allow you to use both enter or space to select the focused button.

UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
Emmanuel

I needed JOptionPane to respond to the return key the same way it responds to the space key and this was how I successfully went about it: I created a new instance of JOPtionPane and called its createDialog method to obtain it's dialog window.

Then I passed its dialog window to the listComponents(java.awt.Container) method below and that did the trick!

    private static index=0;
private static void listComponents(java.awt.Container c){
        if(c==null)return;
        for(java.awt.Component cc:c.getComponents())
            listComponents((java.awt.Container)cc);
        if(c instanceof javax.swing.JButton){
            buttons[index++]=(javax.swing.JButton)c;
            javax.swing.InputMap inputMap=buttons[index-1].getInputMap(javax.swing.JComponent.WHEN_FOCUSED);
            javax.swing.Action spaceAction=buttons[index-1].getActionMap().get(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_SPACE,0));
            inputMap.put(javax.swing.KeyStroke
.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER,0),spaceAction);
        }
    }

In addition to JB Nizet's answer I would like to mention that input handling in java goes this way, if I understand it well:

  1. The components that are in the focussed window
  2. The component that has focus
  3. The component that is an ancestor of the focussed container

All in reverse order of how they were added (I'm not 100% about that but that would be logical). Now, an inputEvent can be consumed. In this case, the JOptionPane registers that enter is pressed, and traverses through the collection of components that have actions bound to the input event. The yes button, as the default button consumes this event so that no other components can do anything with it.

The solution to your problem is that you're going to have to create a custom JDialog. Give it a label for message, a placeholder for an icon, and two buttons. Now make the same constructor as the JOptionPane you are using and give each component the text/icon according to the parameters the constructor receives.

Addition to JB Nizet's answer I want to suggest if you can't using ENTER key to access the Yes, No button than trying to disabled that key. Here some example:

public class NewClass {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    String[] options = {"Yes", "No"};

    int n = JOptionPane.showOptionDialog(frame,
            "Some Question? Press space bar to continue...",
            "",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null, //do not use a custom Icon
            options, //the titles of buttons
            null); // disabled the ENTER Key

    System.out.println(n);

    if (n == JOptionPane.YES_OPTION) {
        System.out.println("Yes");
    } else {
        System.out.println("No");
    }


    System.exit(1);
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!