JOptionPane Passing Custom Buttons

喜欢而已 提交于 2019-11-27 09:33:47
MadProgrammer

In the example I linked to you previous question, the buttons use the JOptionPane#setValue method to set the return value. This allows you to continue using the API as normal, while providing you with the customisation your after.

            final JButton okay = new JButton("Ok");
            okay.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane pane = getOptionPane((JComponent)e.getSource());
                    // set the value of the option pane
                    pane.setValue(JOptionPane.OK_OPTION);
                }
            });

Take a closer look at Disable ok button on JOptionPane.dialog until user gives an input

Updated

I've gone back through the code and correct the actionPerformed methods to enable it to return a valid value...

final JButton okay = new JButton("Ok");
okay.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane pane = getOptionPane((JComponent)e.getSource());
        pane.setValue(okay);
    }
});
okay.setEnabled(false);
final JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane pane = getOptionPane((JComponent)e.getSource());
        pane.setValue(cancel);
    }
});

The value returned by the index of the value in the options array (last parameter)

So, for example...

int value = JOptionPane.showOptionDialog(
     null, 
     field, 
     "Get", 
     JOptionPane.YES_NO_OPTION, 
     JOptionPane.QUESTION_MESSAGE, 
     null, 
     new Object[]{okay, cancel}, 
     okay);

If the user clicks the okay button, the return value will be 0, or if they select the cancel button, it will be 1

If you need this complex behavior, consider creating your own JDialog and then displaying it in a modal fashion.

If you have to use a JOptionPane, you can do this by extracting its JDialog and recursively iterating through its components til you find the one you want to disable and disable it:

import java.awt.Component;
import java.awt.Container;
import javax.swing.*;

public class Foo2 {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            doRun();
         }
      });
   }

   public static void doRun() {
      String[] options = {"Button 1", "Button 2", "Button 3"};

      JOptionPane myOptionPane = new JOptionPane("Heres a test message",
            JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, 
            null, options, options[2]);
      JDialog myDialog = myOptionPane.createDialog(null, "My Test");
      myDialog.setModal(true);

      inactivateOption(myDialog, options[1]);

      myDialog.setVisible(true);
      Object result = myOptionPane.getValue();
      // Note: result might be null if the option is cancelled
      System.out.println("result: " + result);

      System.exit(0); // to stop Swing event thread
   }

   private static void inactivateOption(Container container, String text) {
      Component[] comps = container.getComponents();
      for (Component comp : comps) {
         if (comp instanceof AbstractButton) {
            AbstractButton btn = (AbstractButton) comp;
            if (btn.getActionCommand().equals(text)) {
               btn.setEnabled(false);
               return;
            }
         } else if (comp instanceof Container) {
            inactivateOption((Container) comp, text);
         }
      }

   }
}

However for myself, I'd just create a JDialog.

You don't have to define your buttons explicitly.

int result = JOptionPane.showOptionDialog(this, "Are you sure you want to...?", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Yes", "No" }, JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
  ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!