Setting component focus in JOptionPane.showOptionDialog()

前端 未结 8 1503
广开言路
广开言路 2020-12-10 13:59

In order to have custom button captions in an input dialog, I created the following code:

String key = null;
JTextField txtKey = new JTextField();        
in         


        
8条回答
  •  爱一瞬间的悲伤
    2020-12-10 14:13

        public static String getPassword(String title) {
            JPanel panel = new JPanel();
            final JPasswordField passwordField = new JPasswordField(10);
            panel.add(new JLabel("Password"));
            panel.add(passwordField);
            JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
                @Override
                public void selectInitialValue() {
                    passwordField.requestFocusInWindow();
                }
            };
            pane.createDialog(null, title).setVisible(true);
            return passwordField.getPassword().length == 0 ? null : new String(passwordField.getPassword());
        }
    

提交回复
热议问题