当我在 statechange 上显示对话框时,JCheckbox 会更改状态两次,如何修复?

发布于 2024-11-03 19:20:41 字数 1148 浏览 0 评论 0原文

我有一个复选框,当用户选择它时,应该生成一个包含更多信息的对话框,并根据用户的反应执行某些操作。我的代码基本上如下所示:

private void onItemStateChanged(java.awt.event.ItemEvent evt) {
    System.out.println("STATE CHANGED!");//TODO debug code
    if (evt.getStateChange() == ItemEvent.SELECTED) {
        int returnVal = JOptionPane.showConfirmDialog(this, "blablatext");
        if (returnVal == JOptionPane.OK_OPTION) {
            this.field1TF.setText("");
            this.field1TF.setEditable(false);
            this.field2TF.setText("");
            this.filed2TF.setEditable(false);
        }else if(returnVal == JOptionPane.NO_OPTION){
            this.field1TF.setText("");
            this.field1TF.setEditable(false);
            this.field2TF.setText("");
            this.field2TF.setEditable(false);
        }
    } else if(evt.getStateChange() == ItemEvent.DESELECTED){
        this.field1TF.setEditable(true);
        this.field2TF.setEditable(true);
    }
}

我现在的问题是,当我单击它时,我的复选框总是更改状态两次。 它在某种程度上与 JOptionPane.showConfirmDialog 有关,因为如果我将其注释掉,它就会按预期工作。 我是否没有意识到我应该关心的一些简单的事情,或者我必须做什么才能得到我想要的反应? (用户单击复选框 -> 被问到一个问题 -> 选择是/否/取消 -> 程序相应地执行操作)

I have a checkbox that, when the user selects it, should spawn a dialog with further info, and upon reaction from the user, do something. My code looks basically like this:

private void onItemStateChanged(java.awt.event.ItemEvent evt) {
    System.out.println("STATE CHANGED!");//TODO debug code
    if (evt.getStateChange() == ItemEvent.SELECTED) {
        int returnVal = JOptionPane.showConfirmDialog(this, "blablatext");
        if (returnVal == JOptionPane.OK_OPTION) {
            this.field1TF.setText("");
            this.field1TF.setEditable(false);
            this.field2TF.setText("");
            this.filed2TF.setEditable(false);
        }else if(returnVal == JOptionPane.NO_OPTION){
            this.field1TF.setText("");
            this.field1TF.setEditable(false);
            this.field2TF.setText("");
            this.field2TF.setEditable(false);
        }
    } else if(evt.getStateChange() == ItemEvent.DESELECTED){
        this.field1TF.setEditable(true);
        this.field2TF.setEditable(true);
    }
}

My problem now is, that my checkbox changes state always twice when I click on it.
It somehow has to do with the JOptionPane.showConfirmDialog because if I comment that out, it works as intended.
Am I not aware of something simple I should care about here, or what do I have to do to get my desired reaction? (User clicks checkbox -> is asked a question -> chooses YES/NO/Cancel -> program acts accordingly)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吻风 2024-11-10 19:20:41

eeerggrrh,你必须看看 ItemListener,并使用这个演示....

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** @see http://stackoverflow.com/questions/5806712 */
public class ComponentEventDemo extends JPanel
    implements ActionListener, ComponentListener, ItemListener {

    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JTextArea display;
    private JLabel label;
    private String newline = "\n";
    private JTextField field1TF;
    private JTextField field2TF;
    private JCheckBox checkbox;

    public ComponentEventDemo() {
        super(new BorderLayout());
        display = new JTextArea();
        display.setEditable(false);
        JPanel panel = new JPanel(new GridLayout(0, 4));
        field1TF = new JTextField();
        field1TF.setDisabledTextColor(Color.red);
        field2TF = new JTextField();
        field2TF.setDisabledTextColor(Color.red);
        label = new JLabel("This is a label", JLabel.CENTER);
        label.addComponentListener(this);
        checkbox = new JCheckBox("Label visible", true);
        checkbox.addActionListener(this);
        checkbox.addComponentListener(this);
        checkbox.addItemListener(this);
        panel.add(checkbox);
        panel.add(label);
        panel.add(field1TF);
        panel.add(field2TF);
        panel.addComponentListener(this);
        JScrollPane scrollPane = new JScrollPane(display);
        scrollPane.setPreferredSize(new Dimension(650, 200));
        frame = new JFrame("ComponentEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.add(panel, BorderLayout.SOUTH);
        frame.addComponentListener(this);
        frame.setLocation(200, 200);
        frame.pack();
        frame.setVisible(true);
    }

    public void itemStateChanged(ItemEvent evt) {
        System.out.println("STATE CHANGED!");//TODO debug code
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            int returnVal = JOptionPane.showConfirmDialog(frame, "blablatext");
            if (returnVal == 0) {
                field1TF.setText("SELECTED - OK btn");
                field1TF.setEditable(false);
                field2TF.setText("SELECTED - OK btn");
                field2TF.setEditable(false);
            } else if (returnVal == 1) {
                field1TF.setText("SELECTED - canc btn");
                field1TF.setEditable(true);
                field2TF.setText("SELECTED - canc btn");
                field2TF.setEditable(true);
            }
        } else if (evt.getStateChange() == ItemEvent.DESELECTED) {
            field1TF.setEditable(true);
            field1TF.setText("DESELECTED");
            field2TF.setEditable(true);
            field2TF.setText("DESELECTED");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        boolean selected = checkbox.isSelected();
        System.out.println("Checkbox "
            + (selected ? "is" : "is not") + " selected.");
            if (selected) {
                //some stuff
            } else {
                //some reversal stuff
            }
    }

    protected void displayMessage(String message) {
        display.append(message + newline);
        display.setCaretPosition(display.getDocument().getLength());
    }

    @Override
    public void componentHidden(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Hidden");
    }

    @Override
    public void componentMoved(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Moved");
    }

    @Override
    public void componentResized(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Resized ");
    }

    @Override
    public void componentShown(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Shown");

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComponentEventDemo ced = new ComponentEventDemo();
            }
        });
    }
}

eeerggrrhh, you have to look at ItemListener, and by using this demo ....

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** @see http://stackoverflow.com/questions/5806712 */
public class ComponentEventDemo extends JPanel
    implements ActionListener, ComponentListener, ItemListener {

    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JTextArea display;
    private JLabel label;
    private String newline = "\n";
    private JTextField field1TF;
    private JTextField field2TF;
    private JCheckBox checkbox;

    public ComponentEventDemo() {
        super(new BorderLayout());
        display = new JTextArea();
        display.setEditable(false);
        JPanel panel = new JPanel(new GridLayout(0, 4));
        field1TF = new JTextField();
        field1TF.setDisabledTextColor(Color.red);
        field2TF = new JTextField();
        field2TF.setDisabledTextColor(Color.red);
        label = new JLabel("This is a label", JLabel.CENTER);
        label.addComponentListener(this);
        checkbox = new JCheckBox("Label visible", true);
        checkbox.addActionListener(this);
        checkbox.addComponentListener(this);
        checkbox.addItemListener(this);
        panel.add(checkbox);
        panel.add(label);
        panel.add(field1TF);
        panel.add(field2TF);
        panel.addComponentListener(this);
        JScrollPane scrollPane = new JScrollPane(display);
        scrollPane.setPreferredSize(new Dimension(650, 200));
        frame = new JFrame("ComponentEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.add(panel, BorderLayout.SOUTH);
        frame.addComponentListener(this);
        frame.setLocation(200, 200);
        frame.pack();
        frame.setVisible(true);
    }

    public void itemStateChanged(ItemEvent evt) {
        System.out.println("STATE CHANGED!");//TODO debug code
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            int returnVal = JOptionPane.showConfirmDialog(frame, "blablatext");
            if (returnVal == 0) {
                field1TF.setText("SELECTED - OK btn");
                field1TF.setEditable(false);
                field2TF.setText("SELECTED - OK btn");
                field2TF.setEditable(false);
            } else if (returnVal == 1) {
                field1TF.setText("SELECTED - canc btn");
                field1TF.setEditable(true);
                field2TF.setText("SELECTED - canc btn");
                field2TF.setEditable(true);
            }
        } else if (evt.getStateChange() == ItemEvent.DESELECTED) {
            field1TF.setEditable(true);
            field1TF.setText("DESELECTED");
            field2TF.setEditable(true);
            field2TF.setText("DESELECTED");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        boolean selected = checkbox.isSelected();
        System.out.println("Checkbox "
            + (selected ? "is" : "is not") + " selected.");
            if (selected) {
                //some stuff
            } else {
                //some reversal stuff
            }
    }

    protected void displayMessage(String message) {
        display.append(message + newline);
        display.setCaretPosition(display.getDocument().getLength());
    }

    @Override
    public void componentHidden(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Hidden");
    }

    @Override
    public void componentMoved(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Moved");
    }

    @Override
    public void componentResized(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Resized ");
    }

    @Override
    public void componentShown(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Shown");

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComponentEventDemo ced = new ComponentEventDemo();
            }
        });
    }
}
山田美奈子 2024-11-10 19:20:41

添加 ItemListener 并检查 isSelected 对我来说是这样:

        checkbox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if(checkbox.isSelected()){
                        System.out.println(checkbox.getText() + " ++ " + checkbox.isSelected());
                        //do stuff
                    }else{
                        System.out.println(checkbox.getText() + " -- " + checkbox.isSelected());
                        //do other stuff
                    }

                }
        });

Adding an ItemListener and checking for isSelected did it for me :

        checkbox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if(checkbox.isSelected()){
                        System.out.println(checkbox.getText() + " ++ " + checkbox.isSelected());
                        //do stuff
                    }else{
                        System.out.println(checkbox.getText() + " -- " + checkbox.isSelected());
                        //do other stuff
                    }

                }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文