当我将setSelected()
放入JCheckBox
时,有人可以向我解释为什么我丢失了JOptionPane
选择ItemListener
的选项?
这是一个错误吗?
很奇怪,如果此过程因invokeLater()
而延迟,setSelected()
正如我预期的那样正常工作。
来自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();
}
});
}
}
答案 0 :(得分:4)
这是Oracle Bug ID:6924233确认的已知错误JOptionPane
显然导致使用复选框值= false生成另一个事件。
建议的解决方法是使用JOptionPane
实例化invokeLater
。
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)
答案 1 :(得分:2)
在Mac OS X& Ubuntu我没有看到任何区别:从DESELECTED
状态开始,我点击复选框。我看到立即出现复选标记,然后是选项窗格。无论是否有Runnable
,我都会得到相同的结果。
在Windows上,结果如上所述,但是当选项窗格到达前台时,我看到一个微小的复选标记闪烁。在模拟器中更容易看到效果,例如VirtualBox,这会降低速度。排队Runnable
可恢复正常操作。