Hi all,

I'm a beginner in JAVA studying SWING. I learned that if we want to handle an event for a event source(say JButton) we should implement corresponding Event Listener(say ActionListener) .
I also understood that we should register the Listener with that event source.

With that knowledge I tried this program

import javax.swing.*;
import java.awt.event.*;

public class SimpleGui2 implements MouseListener,ActionListener {
   SimpleGui2 insGui;
   JButton button= new JButton("Click me");

     public static void main(String[] args) {
          SimpleGui2 gui = new SimpleGui2();
          gui.start();
     }

   public void start() {
        JFrame frame = new JFrame();
        
        //button.addMouseListener(this);    
        //button.addActionListener(this); 
        button.addActionListener(insGui);
        
        frame.getContentPane().add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        frame.setSize(300,300);
        frame.setVisible(true);
     }
   
   public void actionPerformed(ActionEvent ae) {
        button.setText("Triggered");
   }
   
  
   public void mouseClicked(MouseEvent me) {
      button.setText("I have been clicked");
    }
   public void mouseEntered(MouseEvent me) { }
   public void mouseExited(MouseEvent me) { }
   public void mousePressed(MouseEvent me) { }
   public void mouseReleased(MouseEvent me) { }   
}

but It's is not working as I expected.
or precisely why when we clicked the actionPerformed/mouseClicked method is not invoked ?

if i change the same with argument "this" it's working .
I guess "this" refers to current object (which is also of type SimpleGui2 which implemented ActionListener)

Can any one please clarify me what happens behind the scenes ?

Thanks in advance

You should probably add a button.validate() call after the button.setText() calls. And use "this" not "insGui". The reason nothing is happening when you use "insGui, is because you have not defined it anywhere. And even if you had, It would have it's own "button" object, which is the one you would be setting the text on, not on the "button" object of the one you are pushing the button on. (Or are you purposely trying to get the actions from one JFrame to affect items in another?)

Thanks for your reply masijide ,
Please confirm me what I understood from your reply is correct .so that I feel I can continue to study further.

What I understood from your is :

I registered the button(gui's button) with insGui object so it's(insGui) actionPerformed method will be invoked which in it's(insGui) button object 's setText method is invoked . so I'm not able to see the event triggered(Since in that frame(gui's frame) insGui's button object is not added or insGui button is not added to any frame at all) .

Am I right ?

and also tell me how can I make it to happen as you asked

Or are you purposely trying to get the actions from one JFrame to affect items in another?

That is when I click this button(gui's) insGui's button have to change .

Thanks in advance

You are correct that "this" refers to the current object instance. YWhen you add a listener the parameter is typed to an interface. You can supply an object which implements that interface. The interface merely guarantees that certain methods are available on that object. Here your gui class implements the listener interfaces itself and therefore is a valid listener. You do not need the "insGui" variable at all in your case.

Thanks for your reply masijide ,
Please confirm me what I understood from your reply is correct .so that I feel I can continue to study further.

What I understood from your is :

I registered the button(gui's button) with insGui object so it's(insGui) actionPerformed method will be invoked which in it's(insGui) button object 's setText method is invoked . so I'm not able to see the event triggered(Since in that frame(gui's frame) insGui's button object is not added or insGui button is not added to any frame at all) .

Am I right ?

and also tell me how can I make it to happen as you asked

That is when I click this button(gui's) insGui's button have to change .

Thanks in advance

That would all be correct, if you had defined insGui anywhere, but you didn't. To tell you the truth, I think you should be getting exceptions everytime you push the button (since insGui is not defined). As I said first, you should simply use "this", instead of "insGui".

That would all be correct, if you had defined insGui anywhere, but you didn't.

Yes masijade, I had noticied and corrected after your first reply .

I think you should be getting exceptions everytime you push the button (since insGui is not defined)

No, I think it's due to compiler checks only for the class type since insGui is an reference to SimpleGui2 I didn't get any Expection for that but I got

Exception in thread "main" java.lang.StackOverflowError
        at SimpleGui2.<init>(SimpleGui2.java:5)

I guess that is due to the object keep on creating another object of same type for it .

I resolved that by creating it inside the start() method

Here is the updated program

import javax.swing.*;
import java.awt.event.*;

public class SimpleGui2 implements MouseListener,ActionListener {
   //SimpleGui2 insGui= new SimpleGui2();
   JButton button= new JButton("Click me");

     public static void main(String[] args) {
          
          SimpleGui2 gui = new SimpleGui2();
          gui.start();
     }

   public void start() {
        JFrame frame = new JFrame();
        JFrame frame2 = new JFrame(); 
        SimpleGui2 insGui= new SimpleGui2(); 
        frame2.getContentPane().add(insGui.button);
        //button.addMouseListener(this);    
        //button.addActionListener(this); 
        button.addActionListener(insGui);
        button.addMouseListener(insGui);
        
        frame.getContentPane().add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        frame.setSize(300,300);
        frame.setVisible(true);

        frame2.setSize(700,300);
        frame2.setVisible(true);
        frame2.setTitle("Fram2");  
 
        
     }
   
   public void actionPerformed(ActionEvent ae) {
        System.out.println("Here");
        button.setText("Triggered");
   }
   
  
   public void mouseClicked(MouseEvent me) {
      System.out.println("Mouse event");
      button.setText("I have been clicked");
    }
   public void mouseEntered(MouseEvent me) { }
   public void mouseExited(MouseEvent me) { }
   public void mousePressed(MouseEvent me) { }
   public void mouseReleased(MouseEvent me) { }   
}

Now I clear with the concept and will study further . Once again Thanks a lot for your reply which really helped me to understand the concept very well .

You are correct that "this" refers to the current object instance. YWhen you add a listener the parameter is typed to an interface. You can supply an object which implements that interface. The interface merely guarantees that certain methods are available on that object. Here your gui class implements the listener interfaces itself and therefore is a valid listener. You do not need the "insGui" variable at all in your case.

Ezzaral , thanks for your reply .I studied that an instance of class which implements XXXListener class is to be passed in addXXXListener() method. Which makes to try with different instance of same class that's why I tried something other than "this".Now I'm clear with the concept.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.