Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

fireActionPerformed()

 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at the source code for AbstractButton and I can't exactly figure out how fireActionPerformed() is called when a button is pushed or when doClick() is called. Can anyone shed some light on this?

I see the "button model" has it's own actionPerformed() method inside the Handler class. The Handler class is instantiated with createActionListener() and assigned to the field actionListener. So it appears something has to call actionListener.actionPerfomed() to call fireActionPerformed(). But I don't understand where or how that is done.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IIRC it's called from the UI delegate. Check the source of BasicButtonUI etc. Look for an inner class (possibly named Handler) that implements MouseListener and other interfaces.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Mack!

Mack Wilmot wrote:So it appears something has to call actionListener.actionPerfomed() to call fireActionPerformed().

Actually, it's the other way around. fireActionPerformed iterates through all of the registered ActionListeners, and calls actionPerformed on them. This approach is often used in abstract model classes; see also AbstractTableModel, AbstractListModel and AbstractDocument.
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Welcome to the Ranch, Mack!

Mack Wilmot wrote:So it appears something has to call actionListener.actionPerfomed() to call fireActionPerformed().

Actually, it's the other way around. fireActionPerformed iterates through all of the registered ActionListeners, and calls actionPerformed on them. This approach is often used in abstract model classes; see also AbstractTableModel, AbstractListModel and AbstractDocument.


See this code from AbstractButton, actionPerformed calls fireActionPerformed. This is the Button Model's ActionListener, not the registered ActionListeners.

 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently it's a bit more complex than I thought, but I figured it out.

ButtonModel has its own set of action listeners. The AbstractButton registers an instance of an internal class (Handler) as one single action listener at the model. DefaultButtonModel (implements ButtonModel) also has a method called fireActionPerformed. That one calls actionPerformed on all registered action listeners - one of which is the AbstractButton's handler. This handler then again calls the AbstractButton's fireActionPerformed method which in turn calls actionPerformed on the AbstractButton's registered action listeners. That leaves the question - what calls DefaultButtonModel's fireActionPerformed method. That's done from the button model's setPressed method. That's where my trail sends.

So:
- DefaultButtonModel.setPressed(true)
- DefaultButtonModel.fireActionPerformed(...)
- AbstractButton.Handler.actionPerformed(...)
- AbstractButton.fireActionPerformed(...)
- ActionListener.actionPerformed(...)

And ButtonModel.setPressed(true) is called by AbstractButton.doClick and BasicButtonListener.mousePressed. The latter is the one that's triggered by user interaction.
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Apparently it's a bit more complex than I thought, but I figured it out.

ButtonModel has its own set of action listeners. The AbstractButton registers an instance of an internal class (Handler) as one single action listener at the model. DefaultButtonModel (implements ButtonModel) also has a method called fireActionPerformed. That one calls actionPerformed on all registered action listeners - one of which is the AbstractButton's handler. This handler then again calls the AbstractButton's fireActionPerformed method which in turn calls actionPerformed on the AbstractButton's registered action listeners. That leaves the question - what calls DefaultButtonModel's fireActionPerformed method. That's done from the button model's setPressed method. That's where my trail sends.

So:
- DefaultButtonModel.setPressed(true)
- DefaultButtonModel.fireActionPerformed(...)
- AbstractButton.Handler.actionPerformed(...)
- AbstractButton.fireActionPerformed(...)
- ActionListener.actionPerformed(...)

And ButtonModel.setPressed(true) is called by AbstractButton.doClick and BasicButtonListener.mousePressed. The latter is the one that's triggered by user interaction.


Ah, well done. I was beating my head trying to figure that out. I was curious about where setArmed() and setPressed() was and you cleared that up too. Thank you.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic