Is there a way to only have the OK button in a JOptionPane showInputDialog (and no CANCEL button)?

后端 未结 3 1708
挽巷
挽巷 2020-12-16 05:35

I\'ve seen that this is possible in other types of dialog windows such as \"showConfirmDialog\", where one can specify the amount of buttons and their names; but is this sam

3条回答
  •  臣服心动
    2020-12-16 06:18

    Just add a custom JPanel as a message to JOptionPane.showOptionDialog():

    enter image description here

    String[] options = {"OK"};
    JPanel panel = new JPanel();
    JLabel lbl = new JLabel("Enter Your name: ");
    JTextField txt = new JTextField(10);
    panel.add(lbl);
    panel.add(txt);
    int selectedOption = JOptionPane.showOptionDialog(null, panel, "The Title", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]);
    
    if(selectedOption == 0)
    {
        String text = txt.getText();
        // ...
    }
    

提交回复
热议问题