Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Button ActionListener actionPerformed

  1. #1
    mohsen.noor is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Button ActionListener actionPerformed

    Hello
    In the following code I can't understand how actionPerformed method executes
    I know that addActionListener submit to the my object a listener, but how my object Invoke actionPerformed methods when an action occurs

    Thanks in advance
    my code
    Java Code:
    import javax.swing.*;
    public class MyButton implements ActionListener{
    
       
        JButton buttuon =new JButton();
        JFrame frame = new JFrame();
        public  void go(){
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(buttuon);
            buttuon.addActionListener(new ActionListener() {
                 @Override
        public void actionPerformed(ActionEvent e) {
                    long l =e.getWhen();
                   
                    Object ob1 = e.getSource();
                    String str = ""+ob1;
                    buttuon.setText( e.toString());
        }
            });
            frame.setSize(300, 400);
            frame.setVisible(true);
           
            
        }
        
       
        public static void main(String args[]){
            MyButton butt = new MyButton();
            butt.go();
            //butt.actionPerformed(null);
            
        }
    ==============================================
    this method of AbstractButton class
    Java Code:
     public void addActionListener(ActionListener l) {
            listenerList.add(ActionListener.class, l);
        }
    ==================================================
    This method of javax.​swing.​event.​EventListenerList
    Java Code:
     /**
         * Adds the listener as a listener of the specified type.
         * @param t the type of the listener to be added
         * @param l the listener to be added
         */
        public synchronized <T extends EventListener> void add(Class<T> t, T l) {
            if (l==null) {
                // In an ideal world, we would do an assertion here
                // to help developers know they are probably doing
                // something wrong
                return;
            }
            if (!t.isInstance(l)) {
                throw new IllegalArgumentException("Listener " + l +
                                             " is not of type " + t);
            }
            if (listenerList == NULL_ARRAY) {
                // if this is the first listener added,
                // initialize the lists
                listenerList = new Object[] { t, l };
            } else {
                // Otherwise copy the array and add the new listener
                int i = listenerList.length;
                Object[] tmp = new Object[i+2];
                System.arraycopy(listenerList, 0, tmp, 0, i);
    
                tmp[i] = t;
                tmp[i+1] = l;
    
                listenerList = tmp;
            }
        }
    How JVM check when a button fired
    thanks again

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Button ActionListener actionPerformed

    The JVM doesn't "check" if a button is fired. The mouse generates a click signal, the OS picks it up and figures out what program it should go to, the JVM receives it, the window toolkit decides what widget it should go to, and finally the Swing event loop calls actionPerformed(...) on your widget. This is called event-driven programming. Google that term to learn more.
    mohsen.noor likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. ActionListener on a button with several lines
    By syon in forum AWT / Swing
    Replies: 3
    Last Post: 02-25-2012, 11:06 PM
  2. button actionPerformed in gui interface
    By harland09 in forum New To Java
    Replies: 1
    Last Post: 06-05-2011, 10:52 PM
  3. ActionListener - actionPerformed
    By collwill in forum New To Java
    Replies: 2
    Last Post: 04-21-2011, 06:01 PM
  4. Problem w/ ActionListener on a button
    By qwertyuiop23 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2009, 06:25 AM
  5. Replies: 9
    Last Post: 11-01-2009, 05:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •