0

私の質問は、キーが押されるまでJOptionPane入力ダイアログの[OK]ボタンを一時的に無効にする方法です。

4

1 に答える 1

1

次に例を示します。

JPanel pan = new JPanel(new BorderLayout());
final JTextField txt = new JTextField(10);
final JButton ok = new JButton("OK");
ok.setEnabled(false);

ok.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        String input = txt.getText();
        System.out.println("The input is: " + input);

        /* close the dialog */
        Window w = SwingUtilities.getWindowAncestor(ok);
        if(w != null) w.setVisible(false);
    }
});

txt.getDocument().addDocumentListener(new DocumentListener()
{
    @Override
    public void removeUpdate(DocumentEvent e)
    {
        if(e.getDocument().getLength() == 0) ok.setEnabled(false);
    }

    @Override
    public void insertUpdate(DocumentEvent e)
    {
        if(e.getDocument().getLength() > 0) ok.setEnabled(true);
    }

    @Override
    public void changedUpdate(DocumentEvent e){}
});

pan.add(txt, BorderLayout.NORTH);
JOptionPane.showOptionDialog(null, pan, "The Title", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, new JButton[]{ok}, ok);
于 2012-11-05T09:24:30.763 に答える