0
votes
public class myWindow extends JFrame implements ActionListener{

if i have this code my class will be a JFrame where where I can add component and add actionlistener to them in my constructor as followed

public MyWindow()
     {
      JButton b = new Jbutton("button");
       b.addActionListener(this);
        }

this keyword will work as an anonymous actionlistener object(which is my class) right ?

later on i will override the actionPerformed method with the heading:-

 public void ActionPerformed(ActionEvent ae)
         {      : 
                :
                        }

I really have a big confusion here .. my book says "the listener object invokes an event handler method with the event as an argument "

listener object : this

event handler method :ActionPerformed(ActionEvent ae)

argument: my event is the JButton b .. how come when it's not of EventAction type ? And if so why we are using :

 ae.getActionCommand(); 

I thought it's a method to tell which component fired the event,why we need it when the component is passed as an argument then?

2
"this keyword will work as an anonymous actionlistener object(which is my class) right ?" -- no, this is not an anonymous ActionListener. You're having your view class implement the interface, something many of us recommend against doing. Also most of us recommend against extending JFrame.Hovercraft Full Of Eels
And no, your JButton is not the "event", rather it's the source. The argument here is an ActionEvent object created by Swing that will put a reference to the activating button into the ActionEvent's source property (obtained by calling getSource())Hovercraft Full Of Eels
this is the way that we use from the beginning of the semesterAmani
I'm a student .. and swing is a new concept for me .The way of coding that we use is making it more complicated ..I get full marks but still CANNOT understand the basics of GUI .. can you recommend some kind of tutorial or somethingAmani
"can you recommend some kind of tutorial or something", yes, the official one. I think you're confused about what's an action and how to use themFrakcool

2 Answers

1
votes

this keyword will work as an anonymous actionlistener object(which is my class) right ?

No this will be an Object of class which is implementing the actionsListener. In your case it is "MyWindow".

my event is the JButton b .. how come when it's not of EventAction type ? And if so why we are using :

JButton b is a component not an event. Events describes the changed in state of the source. When users interact with GUI, events are generated like clicking of button, moving the mouse.

Reference from Click here

Event Handling is a mechanism that controls the event and decides what should happen if an event occurs.

Steps involved in event handling:-

  1. The User clicks the button and the event is generated.

  2. Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.

  3. Event object is forwarded to the method of registered listener class.

  4. the method is now get executed and returns.

I thought it's a method to tell which component fired the event,why we need it when the component is passed as an argument then?

Now you would have understood that there could be many buttons registered to the same ActionsListerner. Now to perform different actions for different events, e.getActionCommand() comes handy, it will tell you which button is the source of firing the event. Hope this helps.

I have tried to give you can example of a simple JButton program.

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

    public class ButtonSwing {
        private int numClicks = 0;

        public Component createComponents() {
            //Method for creating the GUI componenets
            final JLabel label = new JLabel("Clicks: " + "0"); //final so that i can access inside inner class
            JButton button = new JButton("Simple Button");
            button.addActionListener(
            //inner class for ActionListener. This is how generally it is done.     
            new ActionListener() {
                           public void actionPerformed(ActionEvent e) {
                               numClicks++;
                               label.setText("Clicks: " + numClicks);
                   System.out.println(e.getActionCommand());
                   System.out.println(e.getModifiers());
                   System.out.println(e.paramString());
             }
            }
           );
            JPanel pane = new JPanel();   //using JPanel as conatiner first.
            pane.setLayout(new FlowLayout());
            pane.add(button);  // adding button to the JPanel.
            pane.add(label);   // adding label to the JPanel.
            return pane;
        }

        public static void main(String[] args) {
            JFrame frame = new JFrame("SwingApplication");
            ButtonSwing obj = new ButtonSwing();
            Component contents = obj.createComponents();
            frame.getContentPane().add(contents);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            frame.pack();  //It will cause the window to be sized to fit the preferred size 
                            //and layouts of its subcomponents. 
            frame.setVisible(true);
        }
    }   
0
votes

Your JButton is a component not an event. Events are generated by some action on components in this case When you click your button, an ActionEvent will be fired and it will be passed to all the listeners who subscribed to that event in this case it is your MyWindow object which is serving as an ActionListener