Search code examples
javaswingjdialogjoptionpane

how to create yes to all option in dialog box through swing


I want to create a dialog box which contain four button

  1. yes
  2. no
  3. yes to all
  4. cancel

I'm able to create dialog but I am unable to add action in yes to all button.

Object[] options = {"Yes", "No", "Yes To All", "Cancel"};
int n = JOptionPane.showOptionDialog(MainGui.appView, "file "+tempFile+ 
              "exits.\n Do you wish to overwrite?", "Question", 
              JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, 
              null, options, options[2]);

Solution

  • Check for value of n. It will be as follows:

    n --> button
    -----------
    0 --> Yes
    1 --> No
    2 --> Yes to all
    3 --> Cancel
    

    You can switch over n and do action respectively.