简体   繁体   English

JOptionPane传递自定义按钮

[英]JOptionPane Passing Custom Buttons

I'm trying to get the value returned by custom buttons passed to JOptionPane. 我试图获取传递给JOptionPane的自定义按钮返回的值。 However the buttons I pass don't return a value at all. 但是我传递的按钮根本不返回值。 Only when the exit button is pressed is a value of -1 returned. 仅当按下退出按钮时,返回的值为-1。 I need this because I am changing the properties of the buttons enabled or disabled. 我需要这个,因为我正在更改启用或禁用按钮的属性。 I assume I need the buttons to return some information to the JOptionPane in some way. 我假设我需要按钮以某种方式将一些信息返回给JOptionPane。 Any idea? 任何想法?

    JButton button1= new JButton("Button 1");
    JButton button2= new JButton("Button 2");

    button1.setEnabled(false);

    int value = JOptionPane.showOptionDialog(null, "Heres a test message", "Test", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{button1, button2}, button1);
    JOptionPane.showMessageDialog(null, "You entered " + value);

Nb This is related to my previous question - JOptionPane Grey Out One Button Nb这与我之前的问题有关 - JOptionPane Gray Out One Button

I tried setting the value of the buttons like you said but they never return OK or CANCEL. 我尝试设置按钮的值,就像你说的那样,但它们永远不会返回OK或CANCEL。

Whenever checking the value of the buttons, they never return the value I set them too. 每当检查按钮的值时,它们永远不会返回我设置它们的值。

    JButton button1= new JButton("Button1");
    JButton button2= new JButton("Button2");

    button1.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);
            }
        });

    button2.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.CANCEL_OPTION);
            }
        });

      if (JOptionPane.showOptionDialog(null, "Pick a button", "Pick", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{button1, button2}, button1) == JOptionPane.OK_OPTION) {
           JOptionPane.showMessageDialog(null, "Button1");
      }
      else{
           JOptionPane.showMessageDialog(null, "Button2");
      }

See above, always I get the button2 popup no matter what. 看到上面,无论如何我总是得到button2弹出窗口。

In the example I linked to you previous question, the buttons use the JOptionPane#setValue method to set the return value. 在我上一个问题链接到的示例中,按钮使用JOptionPane#setValue方法设置返回值。 This allows you to continue using the API as normal, while providing you with the customisation your after. 这样您就可以像往常一样继续使用API​​,同时为您提供自定义功能。

            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 仔细查看JOptionPane.dialog上的Disable ok按钮,直到用户输入

Updated 更新

I've gone back through the code and correct the actionPerformed methods to enable it to return a valid value... 我已经通过代码返回并更正actionPerformed方法以使其返回有效值...

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) options数组中value的索引返回的值(last参数)

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 如果用户单击okay按钮,返回值将为0 ,或者如果他们选择取消按钮,则为1

If you need this complex behavior, consider creating your own JDialog and then displaying it in a modal fashion. 如果您需要这种复杂的行为,请考虑创建自己的JDialog,然后以模态方式显示它。

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: 如果必须使用JOptionPane,可以通过提取其JDialog并递归遍历其组件直到找到要禁用的那个并禁用它来完成此操作:

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. 但是对于我自己,我只是创建一个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) {
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM