Fares
Fares

Reputation: 1600

ActionListener and ActionEvent issue

I'm having a technical issue with my ActionListener. You see, I have one button that has several Actions possible, depending on which MenuItem was clicked before. So I created an ActionListener with an ActionEvent e. "e" is supposed to verify one of 3 conditions (because I have 3 MenuItems - Add, Delete, and Modify) and depending on which one is chosen, the actions that happen are different from one each other.

The problem is that neither of the e.getSource() conditions never get verified (even though I've checked several times that they were)

 private void buttonValidateActionPerformed(java.awt.event.ActionEvent evt){                                              
    // TODO add your handling code here:
  ActionListener l = (ActionEvent e) -> {
     if(e.getSource()==menuItemAdd)
     {
         System.out.println("eureka!");
         buttonResearch.setEnabled(false);
      if (evt.getSource()== buttonValidate)
        {
            
        DataTransac dt = new DataTransac();
        dt.ajouterProgrammeurs("...");
        
        }
     }
     if(e.getSource()==itemDelete)
     {
         if(evt.getSource()== buttonValidate)
         {
       
        DataTransac dt = new DataTransac();
        dt.deleteProgrammers("...");
         
         }
     }
     if(e.getSource()==itemModify)
     {
         if(evt.getSource()==buttonValidate)
         {
             
        DataTransac dt = new DataTransac();
       dt.modifyProgrammeurs("...");
           
         }
     }
      
 
  };
  
  menuItemAdd.addActionListener(l);
  itemDelete.addActionListener(l);
  itemModify.addActionListener(l);
  
  
    
    
  /*
        
   */
   
}                                   

I tried the ".equals()" method, however it did not work as well.

ActionListener l = (ActionEvent e) -> {
     if(e.equals(menuItemAjouter))
     {
         System.out.println("eureka!");
         buttonResearch.setEnabled(false);
      if (evt.getSource()== buttonValidate)
        {
            
        DataTransac dt = new DataTransac();
        
        dt.addProgrammers("...");
        
        }
        ...

After testing the hashing method, I've stumbled accidentally on a weird bug. At first when I click on the "Add" MenuItem then the "Validate" button, nothing happens. However, if I click on the "Validate" button then the "Add MenuItem, the code works... I'm going to try different conditions.

Upvotes: 0

Views: 118

Answers (2)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

That's a very confusing question.

I am going to start by saying that using getSource is usually a bad idea. It couples that action to the component - exactly the problem listeners were introduced to get away from. It's like going back to JDK 1.00 and nobody wants that, only now we've added additional complication. Also Swing tends to use composite components (it's the Composite design pattern).

Let's look at your code.

private void buttonValidateActionPerformed(java.awt.event.ActionEvent evt){
// TODO add your handling code here: ActionListener l = (ActionEvent e) -> {

What is going on here. Would the real action listener please stand up. Put some printfs in (or use a debugger) to make sure the control flow is as expected.

 if(e.getSource()==menuItemAdd)
 {
     System.out.println("eureka!");
     buttonResearch.setEnabled(false);
  if (evt.getSource()== buttonValidate)

I assume somewhere you have also written.

buttonValidate = menuItemAdd;

It's worth checking in your listener that you have everything set up correctly. Try a few printfs at the top.

System.err.println("e.getSource(): "+System.identityHashCode(e.getSource());
System.err.println("buttonValidate: "+System.identityHashCode(buttonValidate));
System.err.println("menuItemAdd: "+System.identityHashCode(menuItemAdd));
System.err.println("itemDelete: "+System.identityHashCode(itemDelete));
System.err.println("itemModify: "+System.identityHashCode(itemModify));

If you've messed up some assignment somewhere this should show up as unexpected object hash codes.

I suggest backing the code out and adding one action listener per action. You shouldn't need these if statements all over the place. Within each listener they may then check state if necessary.

Upvotes: 0

John Bollinger
John Bollinger

Reputation: 180201

I have one button that has several Actions possible, depending on which MenuItem was clicked before.

The source of an ActionEvent fired in response to a button click is the button that was clicked. You cannot use that to inquire about controls with which the user previously interacted. At least, not directly.

The control flow in your GUI is not entirely clear to me, but your alternatives come in several categories:

  • Use different buttons instead of making the same one responsible for diverse actions.
  • If you want to continue with setting some kind of mode via your menu items, then store the current mode in some shared variable accessible to the button's event handler, and have it use that instead of the action source.
  • Or perhaps have your menu items alter which of several for-purpose ActionListeners is installed on the button, so that the choice of effect can be driven by the listener invoked.

Either of the first two is much preferable to the last, which I offer mainly for completeness.

Upvotes: 0

Related Questions