• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to swing. I want to know when the button is registered with action listener interface how is actionPerformed method is called.

For example
Jbutton b=new Jbutton("New");
b.addActionListener(this);

public void actionPerformed(ActionEvent e){
}

I want to know how this actionPerformed method is called.

Thanks
Mary
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Please check the naming policy; you will have to change your name.
Please use more specific titles for your threads so anybody looking at the display can tell what they are about.

Whenever the button is clicked, it fires an ActionEvent (a little like throwing an Exception, but more benign).
The ActionEvent is sent to whichever listener is registered with the button, and that listener sets off its actionPerformed method, with whatever is inside it.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . And if you haven't registered the listener properly, the ActionEvent disappears into the wild blue yonder never to be seen again . . .
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mary,

Originally posted by Mary Kate:

I want to know how this actionPerformed method is called.



This method is called by the operating system in response to the user doing something with the mouse or keyboard (and perhaps other input devices) on that button. In my experience I've never seen code written to call this method explicitly (if that is what you are wondering).

So for example if your program is running on Windows, and you click on a JButton with your mouse, Windows will talk to the JVM giving it details of what just happened. The JVM will build the ActionEvent object and pass it into this method. Then this method will run giving you the chance to do something in response to the user clicking the button.

Hope that gives you a bit of an idea.

Cheers, Jared.
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic