aspose file tools*
The moose likes JavaFX and the fly likes Centralized action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Centralized action class" Watch "Centralized action class" New topic
Author

Centralized action class

Stefan K Larsson
Greenhorn

Joined: Jul 09, 2014
Posts: 10
I have noticed that there is no JavaFX 8 class similar to QAction in Qt which is a centralized user action which can be triggered by many GUI-components. I have myself used it when having the same action in a menu and a toolbar. The action class allows the programmer to handle action events in a single place. Qt's QAction class can also contain an icon for the action such that the widget which is associated with the action can choose whether to show the icon, the text or both.

The documentation for QAction is given here:
http://qt-project.org/doc/qt-5/qaction.html#details

I assume that there is nothing similar in JavaFX 8?
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 539
    
    7
In JavaFX, use EventHandlers or EventFilters to execute same logic on different user actions.

However, regarding icons, I don't think JavaFX has direct equivalent to associate icon with action.
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 157
    
    9
You can set accelerators on MenuItems and on the scene. That is probably the closest thing in JavaFX to a centralized action class. There is a demo of a MenuItem accelerator here.

Many things in JavaFX extend Labeled, which allow you to specify, text or a graphic (icon) display, or both (via the setContentDisplay API). MenuItems also have the ability to set graphics. Though graphics are most often just an ImageView, they can be any kind of node, so you could make the graphic a more complex widget or an animation or something like that if you wished.
Stefan K Larsson
Greenhorn

Joined: Jul 09, 2014
Posts: 10
I have already tried out the accelerators for menus and such. The biggest advantage I find when using QAction is to be able to add a menu item automatically by just adding the action and everything else is resolved. It can be used as the sole argument to a tool bar button constructor to get a button which is bound the the given action. The actions can be stored in a controller which is totally ignorant about who is triggering the action. Just a convenience.
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 157
    
    9
If you are keen and have the time, you could develop an action system as either a stand-alone open source project or as part of ControlsFX.

The ControlsFX developers include guys like Hendrick Ebbers (author of Mastering JavaFX 8 Controls) and Jonathan Giles (main Controls API maintainer for JavaFX), so they could provide some assistance. Developing for ControlsFX allows for a quicker development cycle then the main JavaFX branch, then, once the API is proven, it could be ported into JavaFX proper. I think it is the intention to add such an API to JavaFX based upon comments on https://javafx-jira.kenai.com/browse/RT-30749 (though such comments cannot be treated as commitments).
Stefan K Larsson
Greenhorn

Joined: Jul 09, 2014
Posts: 10
Thanks for pointing me to the ControlsFX project.

I found the Action class which seems to be just about what I am looking for.

Looking closer att the source code I believe that the functionality is there already although the Action API is separated from the rest through ActionUtils. What is needed is to implement those functions into all the controls of JavaFX such that the constructors can consume the Action class and ditch the ActionUtil class. I could of course create such a merge just to learn my way around the code better, but I am not sure who would be interested in the resulting code. It would not fit into ControlsFX since it needs to involve the JavaFX controls as well.

Maybe I should address these questions directly to the ControlsFX mailing list instead.

EDIT:
Adding Action support for FXML might be a design challenge though...
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 157
    
    9
As part of Java 8 update 40 (the feature release after the next feature release), a dialog API will be brought into JavaFX. It is based on the ControlsFX dialogs. The ControlsFX dialogs include Actions. As part of the API design process there have been discussions surrounding bringing the Actions from ControlsFX into the JavaFX core. Here is link to some of those discussions in case you are interested (search for the word "action" in the links):
https://javafx-jira.kenai.com/browse/RT-12643
http://fxexperience.com/2014/06/bringing-dialogs-to-javafx/
http://fxexperience.com/2014/07/javafx-dialogs-update-1/

> Maybe I should address these questions directly to the ControlsFX mailing list instead

Yes, that and the openjfx development mailing list are good places for these discussions and questions.

> Adding Action support for FXML might be a design challenge

FXML is pretty generic and has already has a simple, extensible architecture. It works based upon reflection, so if you just add an action property to the root control classes which accept actions (e.g. buttons, toolbars, menu items) and define a single builder property for the action class, then you can define an FXML element for an action on a control or menu item. So that part should not be very difficult I think. Probably you would also want to add some UI to SceneBuilder provide a bit of custom UI for action attachment, that part would be a bit more work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Centralized action class