wood burning stoves*
The moose likes JavaFX and the fly likes Get Javafx fx:id from java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Get Javafx fx:id from java code" Watch "Get Javafx fx:id from java code" New topic
Author

Get Javafx fx:id from java code

Milorad Kukic
Greenhorn

Joined: Sep 11, 2013
Posts: 3
Is there a way to get fx:id property from FXML file in java code ?

getId method gives me a CSS id. idProperty of Node also contains CSS id....

Same thing with ToggleGroup. Is there a way to have fx:id of toggleGroup from FXML in java code?

P.S. - I dont want to inject these components in controler. I'm trying to build a "generic" controler that will know how to handle multiple forms (FXMLs).
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 147
    
    9
> Is there a way to get fx:id property from FXML file in java code ?

You would have to copy the FXMLLoader and hack it to create a custom version for your purpose.
If your idea is to build 'a "generic" controller that will know how to handle multiple forms (FXMLs)', then you probably want to create a custom FXMLLoader anyway.
I think jrubyfx (https://github.com/jruby/jrubyfx) might allow similar processing, handling the FXML a bit more dynamically than the default Java case, but it likely uses ruby idioms to achieve this.

http://hg.openjdk.java.net/openjfx/8/master/rt/file/361d97db0d1c/modules/fxml/src/main/java/javafx/fxml/FXMLLoader.java
fx:id retrieval in JavaFX code is demonstrated around line 715 of the linked FXMLLoader code.

Also note, that unless you explicitly set the fx:id and the css id to different things, just setting the fx:id will also set the css id, so if you never set css ids explicitly, then you can always get an fx:id through the node's id property which corresponds to the css id.
Milorad Kukic
Greenhorn

Joined: Sep 11, 2013
Posts: 3
Thank you for replay!!!

Also note, that unless you explicitly set the fx:id and the css id to different things, just setting the fx:id will also set the css id, so if you never set css ids explicitly, then you can always get an fx:id through the node's id property which corresponds to the css id.

I noticed this, and my current solution relays on this fact... My problem is when I use radio buttons.
If if of radio button represents value, than my idea was that radio button group represents property. I set Toggle property to all radio buttons to same value, and when I create form, there is no way that I can retrieve name of that ToogleGroup because it is saved as fx:id property.
I don't like idea of building and maintain custom FXMLLoader just to get name of ToggleGroup id.
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 147
    
    9
I see your issue. I have no other suggestions.
Lionel Pere
Greenhorn

Joined: Sep 13, 2013
Posts: 1
Someone ask the same question on Oracle Forum.

I suggest you to recommand it.

I think that Oracle could add this feature in a future version of JavaFX.

It only request to add a fxId properties in the Node class.

https://forums.oracle.com/thread/2573363
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get Javafx fx:id from java code