-
How to Close Combobox.
Hello happy ppl.
Im stuck, how can I close the ComboBox after I have selected an project. It opens the class OpenOrder as it shoud, but the combobox is left open behind the new frame.
Code:
public OpenComboBox() {
String[] projectNames = { "2004", "2003", "2005" };
JComboBox project = new JComboBox(projectNames);
project.setSelectedIndex(2);
project.addActionListener(this);
add(project, BorderLayout.PAGE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String projectNames = (String)cb.getSelectedItem();
if (projectNames.equals("2004")) {
openOrder.OpenOrder();
frame.setVisible(false); <------------
frame.dispose(); <-------------------
}
}
void comboFrame() {
frame.setPreferredSize(new Dimension(200,100));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JComponent newContentPane = new OpenComboBox();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
}
I have tryed diffrent things in actionPerformed. last thing i tryed was to do a frame.dispose();
Would be happy if someone can point me in the rigth direction.
Best Regards / Ocean
-
Use SwingUtilities.invokeLater. That should help.
Code:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
openOrder.OpenOrder();
}
});
-
Hm how can that close my ComboFrame ?
-
Huh?! You've asked how to close the ComboBox.
-
Sorry if Im bad at explaning. Yes. When i open a new project from MainClass, ComboFrame calls and i can select project from the list. When i selected a project a new Frame appears. But the comboFrame is still in the background. :)