• Post Reply Bookmark Topic Watch Topic
  • New Topic

Working with objects and scenebuilder  RSS feed

 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using scenebuilder with objects like tableview and columns do you still need to create those objects with the new keyword in the code or is that done already?

For instance at the top of my program before the start method I have



Then in my start method I can just start working directly with that table like this..?



 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean I do have columns in the table as well. Just trying to clarify my understanding of how scenebuilder and javafx work together.

 
Knute Snortum
Sheriff
Posts: 4203
122
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code injects an object reference tblMainParts into the code, so no, you don't have to (and you should not) create the object with new.  The fx:id in SceneBuilder connects the FXML widget with the code's variable identifier.  Make sense?
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:This code injects an object reference tblMainParts into the code, so no, you don't have to (and you should not) create the object with new.  The fx:id in SceneBuilder connects the FXML widget with the code's variable identifier.  Make sense?


It does, thank you. I was having some issues getting my table to populate - and thought maybe I was misunderstanding something there. So it turns out I needed to create another controller as the main.java file isn't a good file to use as a controller if it's your main entry point. So I created a separate controller - data still isn't populating the way I need it to but it eliminated some of the issues I was having.
 
Knute Snortum
Sheriff
Posts: 4203
122
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!  Post to the JavaFX forum if you have other questions.
 
John Damien Smith
Ranch Hand
Posts: 351
21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the columns in a TableView should also be injected via the @FXML annotation and a matching id in the FXML file for each column.
See the Makery JavaFX tutorial PersonOverviewController java and fxml files for examples:
  https://community.oracle.com/community/java/java_desktop/javafx_2.0_and_later

In general, anything you annotate with @FXML is created and injected by the FXMLLoader and you should never directly assign the reference to a new object or any other object created by your application.

And yes, you are correct, the main Application class should never be also be a Controller class.  The Java/JavaFX launchers create an instance of an Application class.  Any given application should only have a single Application class instance.  The FXMLLoader creates instances of the Controller classes.  So if you make an Application class also a controller, two instances of it will be created, one by the Java/JavaFX launcher and another by the FXMLLoader, this will almost certainly lead to confusion and bugs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!