【问题标题】:Disable ok button on JOptionPane.dialog until user gives an input禁用 JOptionPane.dialog 上的确定按钮,直到用户输入
【发布时间】:2022-01-16 00:01:12
【问题描述】:

我需要用户输入一个名称,并且我想禁用确定按钮,直到给出一些输入。我怎样才能禁用它...?

【问题讨论】:

    标签: java swing joptionpane


    【解决方案1】:

    尝试搜索 Java 的 swinglabsjGoodies 库。他们为您需要的东西内置了类型。

    【讨论】:

      【解决方案2】:

      据我所知,如果不覆盖 JOptionPane,这是不可能的。

      【讨论】:

        【解决方案3】:

        JOptionPane 允许您提供一个组件作为消息窗格以及可以在其上显示的控件/选项。

        如果您将正确的侦听器添加到消息组件,那么您应该能够影响用作选项的控件。

        看看JOptionPane.showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)

        更新

        例如...

        public class TestOptionPane05 {
        
            public static void main(String[] args) {
                new TestOptionPane05();
            }
        
            protected JOptionPane getOptionPane(JComponent parent) {
                JOptionPane pane = null;
                if (!(parent instanceof JOptionPane)) {
                    pane = getOptionPane((JComponent)parent.getParent());
                } else {
                    pane = (JOptionPane) parent;
                }
                return pane;
            }
        
            public TestOptionPane05() {
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        }
        
                        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);
                            }
                        });
        
                        final JTextField field = new JTextField();
                        field.getDocument().addDocumentListener(new DocumentListener() {
                            protected void update() {
                                okay.setEnabled(field.getText().length() > 0);
                            }
        
                            @Override
                            public void insertUpdate(DocumentEvent e) {
                                update();
                            }
        
                            @Override
                            public void removeUpdate(DocumentEvent e) {
                                update();
                            }
        
                            @Override
                            public void changedUpdate(DocumentEvent e) {
                                update();
                            }
                        });
        
                        JOptionPane.showOptionDialog(
                                        null, 
                                        field, 
                                        "Get", 
                                        JOptionPane.YES_NO_OPTION, 
                                        JOptionPane.QUESTION_MESSAGE, 
                                        null, 
                                        new Object[]{okay, cancel}, 
                                        okay);
                    }
                });
            }
        }
        

        【讨论】:

        • 为我工作。谢谢老哥!
        • 我希望你仍然活跃并且可以回答我的问题。为什么我们需要getOptionPanel 方法?我曾尝试为OptionPanel 设置一个变量并使用该变量在ActionListener 内调用setValue,但直到我实现了您的getOptionPanel 方法后它才起作用。
        【解决方案4】:

        我需要用户输入一个名称,并且我想禁用确定按钮,直到给出一些输入。

        错误的做法。

        即define 'what is a name' = 可以是任何东西。

        所以,实际上,您尝试做的不是接受空字符串,

        您在按下“确定”按钮后进行错误检查。

        如果为空 - 弹出错误消息/重复输入请求/确认取消/你想做的任何事情

        【讨论】:

        • 好吧,猜猜看,有时您的客户会想要 Sanziana 所描述的内容。所以去告诉他这是“错误的做法”:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多