Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what happens when an action occurs, and where and how, tracing the code line by line  RSS feed

 
omid azadi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
java says that when an action occures actionPerformed will be invoked . i want to know where it gets invoked?
please help.

 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go and look for the ActionEvent class, and find its inheritance hierarchy. Now look up its superclass and look for its subclasses. You will find there are all sorts of events which can happen. There are events when a GUI becomes visible, comes to the front, is resized, has the mouse travel across it, etc., etc. In fact a GUI fires off hundreds of events per minute or maybe even per second. Each event travels through the different panes of the top level container, and also through all Components with the same location on screen as the event. The events will disappear off the back (root pane) of the top level container and disappear into cyber‑limbo never to be seen again (except possibly by garbage collection programs). But somewhere on that journey, the event can be listened for. Add an ActionListener to a button, and that listener will intercept all action events passing through that button. When that happens, the one method in ActionListener will be called. The process is similar for all sorts of event; you will also find that if you go a bit higher in the inheritance tree for events, you find this and it has all sorts of different subclasses, representing even more kinds of different events for other circumstances.

The different events occur when different things happen. Action Event, which is listened for by Action Listeners, occurs when you click a button with the mouse, or use the enter key inside a text field, or similar actions.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simplistic overview.
You add an ActionListener to a JButton, which adds it to a List<ActionListener> in the button.
When you click the JButton this will eventually result in the fireActionPerformed method of AbstractButton being called.
That method essentially iterates over all the ActionListeners in the List it has and calls the one (actionPerformed) method.

If you want to know where the fireActionPerformed is called from then debug it and stick a breakpoint at the start of the method.  You'll see the call chain for the entire event.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!