简体   繁体   English

为什么JCheckBox上的setSelected失效?

[英]Why does setSelected on JCheckBox lose effect?

Can someone explain to me why I lost the selection (set by setSelected() ) for JCheckBox when I put the JOptionPane into the ItemListener ? 有人可以向我解释为什么当我将JOptionPane放入ItemListener时会丢失对JCheckBox的选择(由setSelected()设置setSelected()的原因吗? Is this a bug ? 这是一个错误吗?

It is curious, that if this process is delayed with invokeLater() , setSelected() works correctly as I expected. 很好奇的是,如果此过程因invokeLater()而延迟,则setSelected()按我预期的那样正常工作。

在此处输入图片说明在此处输入图片说明

from SSCCE 来自SSCCE

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

public class ComponentEventDemo extends JPanel
        implements ComponentListener, ItemListener {

    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JTextArea display;
    private String newline = "\n";
    private JTextField field1;

    public ComponentEventDemo() {
        super(new BorderLayout());
        display = new JTextArea(10, 25);
        display.setEditable(false);
        JPanel panel = new JPanel(new GridLayout(0, 2));
        field1 = new JTextField();
        field1.setDisabledTextColor(Color.red);
        JCheckBox checkbox = new JCheckBox("Label visible", true);
        checkbox.addItemListener(this);
        panel.add(checkbox);
        panel.add(field1);
        panel.addComponentListener(this);
        JScrollPane scrollPane = new JScrollPane(display);
        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!");
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            //javax.swing.SwingUtilities.invokeLater(new Runnable() {

            //public void run() {
            int returnVal = JOptionPane.showConfirmDialog(display,
                    "Bla Bla Bla Text");
            if (returnVal == JOptionPane.OK_OPTION) {
                field1.setText("SELECTED - OK btn");
            } else if (returnVal == JOptionPane.NO_OPTION) {
                field1.setText("SELECTED - NO btn");
            } else if (returnVal == JOptionPane.CANCEL_OPTION) {
                field1.setText("SELECTED - Cancel btn");
            } else if (returnVal == JOptionPane.CLOSED_OPTION) {
                field1.setText("SELECTED - Close btn");
            }
            //}
            //});
        } else if (evt.getStateChange() == ItemEvent.DESELECTED) {
            field1.setText("DESELECTED");
        }
    }

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

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

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

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

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

    }

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

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

It is a known bug as acknowledged by Oracle Bug ID:6924233 The JOptionPane apparently causes another event to be generated with a check box value = false. 它是一种已知的错误通过为已确认Oracle错误ID:6924233JOptionPane显然会导致与复选框值=假来生成另一事件。

The recommended fix is to instantiate the JOptionPane using invokeLater . 推荐的修复方法是使用invokeLater实例化JOptionPane

Submitted On 09-MAR-2010

The change is in the BasicButtonListener -  Method focusLost()

In 1.6.0_18 it is

       ...
       ButtonModel model = b.getModel();
       model.setPressed(false);
       model.setArmed(false);

in 1.6.0_10 it was

       ...
       ButtonModel model = b.getModel();
       model.setArmed(false);
       model.setPressed(false);

(The order of the statements changed)

And a setPressed(false) with armed==true leads on an ToggleButton like 
the JCheckBox to a change of the selection (see ToggleButtonModel) 

On Mac OS X & Ubuntu I don't see any difference: Starting from the DESELECTED state, I click on the checkbox. 在Mac OS X和Ubuntu上,我看不到任何区别:从DESELECTED状态开始,单击复选框。 I see the check mark appear immediately, followed by the option pane. 我看到复选标记立即出现,然后出现选项窗格。 I get the same result with or without the Runnable . 无论是否使用Runnable我都会得到相同的结果。

On Windows, the result is as described, but I see a tiny flicker of the check mark as the option pane comes to the foreground. 在Windows上,结果如前所述,但是随着选项窗格的出现,我看到了复选标记的微小闪烁。 The effect is easier to see in an emulator, such as VirtualBox , which can slow things down. 在仿真器(如VirtualBox )中更容易看到效果,这会使速度变慢。 Queueing the Runnable restores normal operation. Runnable排队可以恢复正常操作。

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

相关问题 javax.swing.JCheckBox setSelected不是从GUI调用 - javax.swing.JCheckBox setSelected is not calling from GUI 为什么thenCallRealMethod()在这里丢失参数? - Why does thenCallRealMethod() lose the arguments here? JCheckBox在运行时不会取消选择 - JCheckBox does not deselect during runtime 为什么即使使用setSelected(true)也未选择JRadioButton - why JRadioButton is not selected even by using setSelected(true) 为什么重新启动的Java程序会失去键盘焦点? - Why does my restarted Java program lose keyboard focus? 为什么重启应用后 Room 数据库会丢失数据? - Why does Room database lose data after restarting the app? MongoDB重新启动后,为什么Java App会失去连接? - Why does the Java App lose connection after MongoDB restarted? isSelected()无法识别JRadioButton和JCheckBox被选中 - isSelected() does not recognize JRadioButton and JCheckBox are selected ListSelectionListener 在调用 setSelected 方法时不会触发事件 - ListSelectionListener does not fire events when calling setSelected methods JMenuItem.setSelected() 不会改变所选项目的外观? - JMenuItem.setSelected() does not alter appearance of selected item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM