Setting Mnemonics and Hot Keys for a JOptionPane Dialog

核能气质少年 提交于 2019-12-05 11:49:35
broschb

You can create your JOptionPane, and then loop through the components of the pane (children etc.) checking to see if any components are instanceof JButton, and if so check the text, and set the proper mnemonic.

JOptionPane p = new JOptionPane();
Component[] c = p.getComponents();

Make use of UIManager as follows:

UIManager.put("OptionPane.okButtonMnemonic", "79");  // for Setting 'O' as mnemonic
UIManager.put("OptionPane.cancelButtonMnemonic", "67"); // for Setting 'C' as mnemonic

Send the buttons in as parameters instead of Strings

    JButton button1 = new JButton( "<html>" + nextQuestion1 + "</html>");
    button1.setMnemonic('a');
    JButton button2 = new JButton(nextQuestion2 + "VUHU");
    JButton button3 = new JButton(abort);
    Object[] possibleValues = new Object[]{button1,button2,button3};
    int selectedValue = showOptionDialog(owner, question, possibleValues);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!