So my assignment says to create a sequential file. My professor gave me this simple code for the actionperformed:
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 COMBOBOXEZ*****
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);
}
}
}
it basically says if you hit anything but the "Done" button, it creates a sequential file. What should i do to choose what each action will do? i did a jcombobox with countries and states, and when i choose a country, it creates the file and then it gets me to the country's states.Hope you can help me out thanks.
I'm not sure I entirely understand your problem, but...
You can use:
ActionEvent#getSource
to get the source component of the event. Assuming that you can reference the other components, you can simply use this to compare them, for exampleif (e.getSource( ) == btnDone) {
ActionEvent#getActionCommand
which returns theactionCommand
associated with the component (set via thesetActionCommand
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);
}
}
来源:https://stackoverflow.com/questions/34863894/how-to-use-actionperformed-actionevent-e-with-more-that-one-button