[英]JComboBox gives IllegalComponentStateException and I don't understand why
I am writing a GUI for a simple program that mimics a block of RAM and I wanted to use a JComboBox to let the user select a command option from a drop down list. 我正在为一个模仿RAM块的简单程序编写GUI,我想使用JComboBox来让用户从下拉列表中选择命令选项。 However, whenever I try to select an option from the list, it gives me an IllegalComponentStateException and says that the component must be showing on the screen to determine its location. 但是,每当我尝试从列表中选择一个选项时,它都会给我IllegalComponentStateException,并说必须在屏幕上显示该组件才能确定其位置。 Here is the code for creating the main screen. 这是用于创建主屏幕的代码。
public static void createMainMenu(){
mainFrame.getContentPane().removeAll();
mainFrame.setSize(700 , 500);
mainFrame.setBackground(Color.WHITE);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new GridLayout(3 , 3));
JPanel ramStatus = new JPanel();
ramStatus.setBackground(Color.WHITE);
ramStatus.setSize(200 , 200);
JLabel heading = new JLabel("Please Select An Option to Continue.");
JComboBox<String> userChoice = new JComboBox<String>();
for(int i = 0; i < choices.length; i++)
userChoice.addItem(choices[i]);
userChoice.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
JComboBox combo = (JComboBox)e.getSource();
choice = combo.getSelectedItem().toString();
}});
JButton enter = new JButton("Enter");
enter.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
handleSelection(choice);
}});
mainFrame.add(new JPanel());
mainFrame.add(userChoice);
mainFrame.add(enter);
mainFrame.add(new JPanel());
mainFrame.add(heading);
mainFrame.add(new JPanel());
mainFrame.add(new JPanel());
mainFrame.add(ramStatus);
mainFrame.add(new JPanel());
//Set the frame visible.
mainFrame.setVisible(true);
}
I should mote that choice is a static String variable declared after class declaration. 我应该选择的是在类声明之后声明的静态String变量。 Can anyone explain why this is happening and how I can fix it? 谁能解释为什么会这样,我该如何解决?
** * ** * ** * **** EDIT * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** ** * ** * ** * **** 编辑 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ***
Here is the error message generated when I select a new option from the drop down menu. 这是当我从下拉菜单中选择一个新选项时生成的错误消息。 The exception is thrown before I hit enter, so it has nothing to do with outside methods. 在我按Enter键之前会抛出异常,因此它与外部方法无关。
java.awt.IllegalComponentStateException: component must be showing on the screen to determine
its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2044)
at java.awt.Component.getLocationOnScreen(Component.java:2018)
at sun.lwawt.macosx.CAccessibility$22.call(CAccessibility.java:390)
at sun.lwawt.macosx.CAccessibility$22.call(CAccessibility.java:388)
at sun.lwawt.macosx.LWCToolkit$CallableWrapper.run(LWCToolkit.java:532)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:241)
at sun.lwawt.macosx.LWCToolkit$CPeerEvent.dispatch(LWCToolkit.java:689)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Why do you use addActionListener
to listen to ComboBox item selection? 为什么使用addActionListener
来侦听ComboBox项的选择? Have you tried using addItemListener
instead? 您是否尝试过使用addItemListener
代替?
Can anyone explain why this is happening and how I can fix it?
No, because you do not give complete information. 不,因为您没有提供完整的信息。
Since the question is not worth making a runnable example to you, I did it: I added the vars mainFrame, choices, and choice, and gave string values to the choices array, I eliminated the "String" parameter from JComboBox type (running 1.6, JComboBox is not generic), and I commented out the call to handleSelection. 由于该问题不值得为您提供一个可运行的示例,因此我做到了:我添加了vars mainFrame,choices和choice,并将字符串值提供给choices数组,因此从JComboBox类型中删除了“ String”参数(运行1.6 ,JComboBox不是通用的),并且我注释掉了对handleSelection的调用。 I start the UI, and can select values from the drop-down multiple times with no error. 我启动了UI,可以多次从下拉菜单中选择值,而不会出现错误。
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ComboBoxPlay
{
private static JFrame mainFrame = null;
private static String[] choices = {"one", "two", "three" };
private static String choice = null;
public static void main(String[] args)
{
mainFrame = new JFrame();
createMainMenu();
}
public static void createMainMenu(){
mainFrame.getContentPane().removeAll();
mainFrame.setSize(700 , 500);
mainFrame.setBackground(Color.WHITE);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new GridLayout(3 , 3));
JPanel ramStatus = new JPanel();
ramStatus.setBackground(Color.WHITE);
ramStatus.setSize(200 , 200);
JLabel heading = new JLabel("Please Select An Option to Continue.");
JComboBox userChoice = new JComboBox();
for(int i = 0; i < choices.length; i++)
userChoice.addItem(choices[i]);
userChoice.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
JComboBox combo = (JComboBox)e.getSource();
choice = combo.getSelectedItem().toString();
}});
JButton enter = new JButton("Enter");
enter.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
// handleSelection(choice);
}});
mainFrame.add(new JPanel());
mainFrame.add(userChoice);
mainFrame.add(enter);
mainFrame.add(new JPanel());
mainFrame.add(heading);
mainFrame.add(new JPanel());
mainFrame.add(new JPanel());
mainFrame.add(ramStatus);
mainFrame.add(new JPanel());
//Set the frame visible.
mainFrame.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.