How to use actionPerformed( ActionEvent e ) with more that one button?

我怕爱的太早我们不能终老 提交于 2019-12-04 06:03:00

问题


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.


回答1:


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 example if (e.getSource( ) == btnDone) {
  • ActionEvent#getActionCommand which returns the 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




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!