为什么 JCheckBox 上的 setSelected 失去作用?
有人可以向我解释为什么当我将 JOptionPane
放入 时,我丢失了
JCheckBox
的选择(由 setSelected()
设置)项目监听器? 这是一个错误吗?
奇怪的是,如果这个过程被 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();
}
});
}
}
Can someone explain to me why I lost the selection (set by setSelected()
) for JCheckBox
when I put the JOptionPane
into the ItemListener
?
Is this a bug ?
It is curious, that if this process is delayed with invokeLater()
, setSelected()
works correctly as I expected.
from 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();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个由 Oracle Bug ID:6924233 确认的已知错误>JOptionPane 显然会导致生成另一个事件,其中复选框值 = false。
建议的修复方法是使用
invokeLater
实例化JOptionPane
。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.The recommended fix is to instantiate the
JOptionPane
usinginvokeLater
.在 Mac OS X 和Ubuntu 我没有看到任何区别:从
DESELECTED
状态开始,我单击复选框。我看到复选标记立即出现,然后是选项窗格。无论有或没有Runnable
,我都会得到相同的结果。在 Windows 上,结果如所描述的那样,但当选项窗格出现在前台时,我看到复选标记有微小的闪烁。在模拟器中更容易看到效果,例如 VirtualBox,它会减慢速度。对
Runnable
进行排队可恢复正常操作。On Mac OS X & Ubuntu I don't see any difference: Starting from the
DESELECTED
state, I click on the checkbox. I see the check mark appear immediately, followed by the option pane. I get the same result with or without theRunnable
.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. The effect is easier to see in an emulator, such as VirtualBox, which can slow things down. Queueing the
Runnable
restores normal operation.