So my assignment says to create a sequential file. My professor gave me this simple code for the actionperformed:
public void actionPerformed( ActionEvent e
I'm not sure I entirely understand your problem, but...
You can use:
if (e.getSource( ) == btnDone) {
actionCommand
associated with the component (set via the setActionCommand
on supporting components), which provides you means of determining the type of event, without needing a reference to the source components, this is also useful when you might have a "common" action that can triggered in multiple different ways.Now, generally speaking, when actionPerformed
is called, you want to determine what triggered the action and take appropriate action, so the case of your code, you might do something more like...
if (e.getSource() == comboBox_1) {
String country = (String) comboBox_1.getSelectedItem();
Object o = states.get(country);
if (o == null) {
comboBox_2.setModel(new DefaultComboBoxModel());
} else {
comboBox_2.setModel(new DefaultComboBoxModel((String[]) o));
}
//****DONE WITH THE STATE AND COUNTRY COMBOBOXEZ*****
addRecord();
} else if (e.getSource() == btnDone) {
try {
output.close();
} catch (IOException io) {
System.err.println("File not closed properly\n"
+ e.toString());
System.exit(1);
}
System.exit(0);
}
This is a rather "old" way to approach designing ActionListener
, which harks back to the days before inner/anonymous classes, where is was just simpler to create a single ActionListener
class.
Now days it's generally more preferable to use inner/anonymous classes to generate small, isolated and contextual listeners, which might look something like...
comboBox_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String country = (String) comboBox_1.getSelectedItem();
Object o = states.get(country);
if (o == null) {
comboBox_2.setModel(new DefaultComboBoxModel());
} else {
comboBox_2.setModel(new DefaultComboBoxModel((String[]) o));
}
//****DONE WITH THE STATE AND COUNTRY COMBOBOXEZ*****
addRecord();
}
});
This is a self contained, contextual unit work, designed for a single use of work. Generally it's easier to read and maintain as the context is well defined and you're not trying to tip-toe around a bunch of unrelated work.
If you need something that's more re-usable, then you should have a look at How to Use Actions
I found the solution, it was right in front of my eyes, here it is for future users.
public void actionPerformed(ActionEvent e){
//FOR STATE AND COUNTRY
String country = (String)comboBox_1.getSelectedItem();
Object o = states.get(country);
if (o == null){
comboBox_2.setModel(new DefaultComboBoxModel());
}
else{
comboBox_2.setModel(new DefaultComboBoxModel((String[])o));
}
//****DONE WITH THE STATE AND COUNTRY COMBOBOXES*****
// this was the solution for my problem
if(e.getSource==buttonToAddRecords){
addRecord( ) ;
}
if(e.getSource( ) == btnDone){
try{
output.close( );
}
catch(IOException io){
System.err.println("File not closed properly\n" +
e.toString( ));
System.exit(1);
}
System.exit(0);
}
}