File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Javafx fx:id from java code

 
Milorad Kukic
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 235
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 235
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see your issue. I have no other suggestions.
 
Lionel Pere
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic