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

后端 未结 3 1699
挽巷
挽巷 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:08

    JOptionPane.showInputDialog() returns the string the user has entered if the user clicks "OK" and returns null otherwise. See this:

    Returns: user's input, or null meaning the user canceled the input

    You can't do this using showInputDialog()

    However, you can use JOptionPane#showOptionDialog():

    Object[] buttons = {"OK"};
    int res = JOptionPane.showOptionDialog(yourFrame,
                       "YourMessage","YourTitle",
                       JOptionPane....,
                       JOptionPane..., null, buttons , buttons[0]);
    

    As @HovercraftFullOfEels stated on the comments, you can add JTextField to the dialog and achieve this.

提交回复
热议问题