2

I have written a small code in java for simpleGUI.

package guidemo1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class GuiDemo1 implements ActionListener{
 JButton button;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        GuiDemo1 gui=new GuiDemo1();
        gui.go();
    }

    public void go()
    {
        JFrame frame=new JFrame();
        button=new JButton();
        frame.getContentPane().add(button);
        button.addActionListener(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        button.setText("I've been clicked");
    }
}

I am newbie to JAVA.I have few questions related to this program.

Can some one explain how actionPerformed method gets executed with out any call?

Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?

Thanks..

Dan D.
  • 32,246
  • 5
  • 63
  • 79
starkk92
  • 5,754
  • 9
  • 43
  • 59
  • Being implements of ActionListener, the actionPerformed() is overrided so, any action goes to actionPerformed() without any call... – Surendra Jnawali Feb 21 '13 at 10:44

5 Answers5

3

Your class implements ActionListener, so you have to define the actionPerformed method. You also call button.addActionListener(this) which registers the current object as the listener for the button press.

When a button press event is triggered, all the listeners will be notified, including the object that you have registered as a listener.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
3

Welcome to an event driven environment.

In this environment, you register "listeners" that wait until something happens and then report back to you via a well defined interface structure.

Basically, what's a happening, is you registered yourself as an interested party to action events that may occur on a button. You've done this by implementing the ActionListener interface. This allows the button, when an action is performed to call back to your actionPerformed method at some time in the future.

This is commonly known as an observer pattern.

You might find Writing Event Listeners a useful read

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

As for the second question, Java is a reference language. All objects reside on the heap and are alive as long as someone holds the reference to them.

In Swing, the case is a little bit more complicated. The Swing library holds reference to all GUI elements (such as JFrame and JButton), so that's why they don't get deleted, even if you don't hold reference to them any more.

As for the first question, look at what the main thread does - it creates a new object GuiDemo1, calls the go() method which creates a JFrame, and displays that frame. There is a special Swing thread, the Event Dispatch Thread, that waits in the background for events in the GUI to happen. Displaying the frame starts the EDT which then renders the frame and sleeps. The main thread, having completed all the work in the main method then DIES.

Now you have a JFrame displayed and Event Dispatch Thread sleeping. The EDT is now waiting for user interaction (events) with the JFrame - the Swing is event-driven. Each time you interact with the frame, Swing creates an event for it and wakes the EDT to handle the event. So for example, if you click a button, the EDT wakes, animates the button "click" and calls all action listeners. Thats when your method actionPerformed gets called.

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
1

Can some one explain how actionPerformed method gets executed with out any call?

The GUI framework Swing runs action handling code in the background. Whenever a button is pressed or the user interacts with the GUI in some other way, Swing will notify your application through one of many Listener interfaces. In order to receive these events, your class needs to implement the proper Listener interface and be registered as a listener on each component it is interested in.

Your class implements the ActionListener interface and calls addActionListener to register itself to the button. When a button is clicked, Swing will attempt to notify all registered ActionListeners by calling their actionPerformed method. That's how the "magic" happens.

Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?

You add the button to the frame's content pane - this puts the button inside the frame in your layout, not in your code. Because you declare button as an instance variable by putting JButton button; outside any method, it is still accessible from any (non-static) method in that class.

Jacob Raihle
  • 3,720
  • 18
  • 34
0
  • implement ActionListener

  • add ActionListener to your interface

  • add actionPerformed method to your class

  • set a timer and start it in your class

that will do it

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331