aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes fireActionPerformed() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "fireActionPerformed()" Watch "fireActionPerformed()" New topic
Author

fireActionPerformed()

Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

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

Joined: May 03, 2008
Posts: 4546
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Jul 27, 2011
Posts: 88

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

Joined: Oct 27, 2005
Posts: 19696
    
  20

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: fireActionPerformed()