JComboBox ActionListener and Swing Thread safety
I am developing an application that among other things has two JComboBoxes. It is convenient that both combo boxes has the same action listener. Basically at the most upper level I want to create a JFrame class which will contain all the GUI elements, data storage class which will contain internal data needed for application to run, data access class which will work with files and control class which will control the work of all classes. I want control class to be the action listener for the two combo boxes.
Question one: Is there a smarter way to determine which combo box trigered the event than
Code:
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == comboBox1)
{
comboBox1ControlMethod();
}
else if(source == comboBox2)
{
comboBox2ControlMethod();
}
else
{
JOptionPane.showMessageDialog(null, errorString);
}
}
Mister Cay S. Horstmann says in his book Core Java 8 Edition that this is possible way to solve the problem but he doesn't recommend it.
Second question: Imagine that my action listener is written in the above fashion. And imagine that comboBox1ControlMethod and comboBox2ControlMethod launch a separate thread which will gather appropriate data do there work and then terminate.
Let say user make a selection in the comboBox2. comboBox2ControlMethod launches the thread which in turn determine which item from the combo box user selected, then read appropriate files, update the internal data class, and that repaint the frame. By repainting the frame I mean only changing the text in several JTextField classes and changing which item is selected in several JComboBoxes. All this components whose data I change like this don't have ActionListener associated with them. I care about these components contest only when the user decided to save his work.
I read in the API that Swing is not thread save.
Can I launch a separate thread to do this work or do I have to do this work in the same thread that controls the GUI.
If I was unclear tell me and I will try to explain better.
Thanks in advance.