80436

JAVAFX - FXML - Access Loaded FXML Controls from Parent Controller

Question:

Here is what I am trying to accomplish.

/Package A/ /Package A/ApplicationController.java /Package A/Application.fxml

In my Application.fxml file I have a button, and when that button is clicked it loads the following "MyGrid.fxml" file.

/Package B/ /Package B/MyGrid.fxml (has a label #mygridlabelid

The code I am using is:

ContentPane.getChildren().add((Node)FXMLLoader.load(getClass().getResource("/Package B/MyGrid.fxml")));

But the problem is.. even though I am loading the MyGrid.fxml file from the ApplicationController, I cannot access #mygridlabelid from the ApplicationController file. I defined @FXML label mygridlabelid in the ApplicationController.java file, but it doesn't get instantiated :(

How can I do that? Any tricks or hacks around it?

Answer1:

I managed to solve the problem by doing the following... and make sure that the .fxml file does not have fx:controller set. Or else you will run into "Controller value already specified."

FXMLLoader loader = new FXMLLoader(getClass().getResource("/your.fxml")); loader.setController(this); try { ContentPane.getChildren().add((Node)loader.load()); } catch (IOException e){ System.out.println(e.getMessage()); }

Recommend

  • how to call combobox selected item from another class in java?
  • Linking to a specific tab / contentpane with Dojo
  • FOSRestBundle + Symfony 3.4 : rest type not reconized
  • How to switch an AccordionContainer on click?
  • Unable to access controls inside JuiceUI dialog
  • html issues with images in JTable
  • Content Panes in Java
  • Use of java.lang.Class.getResources
  • Referencing class resource in FXML
  • Read .jar file in Java applet
  • getResource() unable to read contents of a directory inside jar
  • Netbeans: Try to load file but not found (Java)
  • using keyEvent from different classes
  • Invisible objects because repaint method - Java Swing
  • How to open a resource file in a WAR using a string pathname?
  • Xamarin: Couldn't find “libmonodroid.so” emulator error
  • Why isn't setIcon displaying image on JButton?
  • JavaFX not finding all system fonts
  • How to extract directory (and sub directories) from jar resource?
  • wrap content in JavaFX
  • How to make object in List eligible for garbage collection?
  • GUI not updating despite use of validate()
  • GridBagLayout padding
  • JSF binding with setValueExpression read-only?
  • logging static methods in a parent class
  • Focus Traversal Policy in TitledPane
  • ApplePay PKPaymentAuthorizationViewController always shows processing
  • xcode don't localize specific strings
  • Read text file that is not in the main package in a runnable jar
  • Swift: Switch statement fallthrough behavior
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal