簡體   English   中英

在JOptionPane.dialog上禁用ok按鈕,直到用戶輸入

[英]Disable ok button on JOptionPane.dialog until user gives an input

我需要用戶輸入一個名稱,我想禁用ok按鈕,直到給出一些輸入。 我怎么能禁用它......?

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

據我所知,如果不重寫JOptionPane這是不可能的。

嘗試搜索適用於Java的swinglabsjGoodies庫。 它們內置了您需要的類型。

我需要用戶輸入一個名稱,我想禁用ok按鈕,直到給出一些輸入。

做錯的方法。

即定義'什么是名字'=可以是任何東西。

那么,你實際上想要做的是不接受一個空字符串,

並且您在按下確定按鈕后'執行錯誤檢查'。

如果為空 - 彈出錯誤消息/重復輸入請求/確認取消/無論你想做什么

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM