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

前端 未结 2 1167
暗喜
暗喜 2021-01-27 08:28

So my assignment says to create a sequential file. My professor gave me this simple code for the actionperformed:

    public void actionPerformed( ActionEvent e          


        
相关标签:
2条回答
  • 2021-01-27 09:09

    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

    0 讨论(0)
  • 2021-01-27 09:27

    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); 
      }
    } 
    
    0 讨论(0)
提交回复
热议问题