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

前端 未结 2 1228
暗喜
暗喜 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: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); 
      }
    } 
    

提交回复
热议问题