In this code:
public class ESM extends Application {
private Stage primaryStage;
#FXML
private ToolBar mainToolBar;
#Override
public void start(final Stage stage) throws Exception {
try{
this.primaryStage = stage;
Parent root = FXMLLoader.load(getClass().getResource("/nz/co/great_ape/esm3/main_window.fxml"));
Scene scene = new Scene(root, 800, 700);
// Setup main stage to be full screen, no min or max buttons.
// TODO: How will this handle multiple screens? Apparently not well :-(
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setTitle("ESM three");
primaryStage.setScene(scene);
primaryStage.show();
System.out.println("This will fail because mainToolBar is null. Why?");
assert mainToolBar != null : "fx:id=\"mainToolBar\" was null check your FXML ";
} catch (Exception ex) {
Logger.getLogger(ESM.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Use initialize() to setup widgets from scenebuilder files, it is
* called by FXMLLoader.
*/
#FXML
public void initialize(){
System.out.println("initialize() But when here all is good and mainToolBar is a ToolBar.");
assert mainToolBar != null : "fx:id=\"mainToolBar\" was null check your FXML ";
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support.
*
* #param args The command line arguments.
*/
public static void main(String[] args) {
launch(args);
}
}
I cant see why it's got a value in the initialise() but in the start it's null. When debuging it's clear that initiialize() is called by FXMLLOader from inside start()
I was going to post the fxml but it does not seem to work as nothig shows in the preview. Any way, it's a real basic file, a BordePane and a ToolBar.
Any clues?
Always create a new class for your FXML Controller, don't try to reuse an Application class as a Controller class.
An Application instance is created by the JavaFX application launcher.
A Controller instance is created by the JavaFX FXML loader.
You don't supply the FXML that you use, but I am going to guess that it has it's Controller class erroneously set to be your application class.
So in your code, what happens is:
An instance of the application is created when you run the program (via the launch method).
In your application start method, you invoke the FXMLLoader, which instantiates a new Controller (in your case a new instance of the application class).
The FXMLLoader injects the #FXML tagged members into the new application object and invokes the initialize on the new object.
But your original application object doesn't know anything about the new application object and hence doesn't have a menu bar set in it.
In summary, to fix this:
Create a new controller class that the FXMLLoader can instantiate.
Change your fxml to reference the new controller class.
If your application really needs to reference the controller, then you can use the getController method on the FXML loader and in your controller class provide public methods to retrieve required elements (like your menu bar). See my answer to Passing Parameters JavaFX FXML for some more examples of this method.
import javafx.scene.control.ToolBar;
import javafx.fxml.FXML;
public class ESMController {
#FXML
private ToolBar mainToolBar;
public ToolBar getMainToolBar() { return mainToolBar; }
#FXML
public void initialize(){
assert mainToolBar != null : "fx:id=\"mainToolBar\" was null check your FXML ";
}
}
Related
This question already has answers here:
JavaFX : Pass parameters while instantiating controller class
(2 answers)
Closed 23 days ago.
Edit: I believe I found the correct answer to my problem after all.
Original Post:
I'm currently trying to create an application with JavaFX and an EventBus-System. To do this, I have to pass the EventBus as constructor argument to other classes when instantiating them. However I don't know how to do this while also using an FXMLLoader to load my .fxml-Files.
My code currently looks somethinng like this:
Main Class
public class MyApplication extends Application {
public void start(Stage stage) throws Exception {
EventBus eventBus = new EventBus();
>>> Here would be code that creates an Object of MainView, passing eventBus as constructor argument. <<<
Scene scene = new Scene(mainView);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This class inherits from BorderPane and I want to create an object of it using fxmlLoader (I think. I'm not sure if it works like that)
puplic class MainView extends BorderPane {
private EventBus eventBus;
public MainView(EventBus eventBus) {
this.eventBus = eventBus;
... other code
}
}
I also have a controller for MainView (don't know if thats important to mention)
public class MainViewController {
>>> several JavaFX Elements like buttons, labels etc and their associated functionalities like onActions and such... <<<<
}
And of course there is an .fxml-File that contains the actual design of the MainView that I created with SceneBuilder, but I won't post it here since it doesn't seem necessary. But I sould probably mention that this .fxml-File contains a BorderPane as it's highest node. I think that makes sense, since my MainView extends BorderPane.
My Problem is that I ever created my own class that extends BorderPane and needs a Constructor parameter before and I don't really know how to create an instance of it.
In the past I did something like this:
FXMLLoader loader = new FXMLLoader();
BorderPane root = loader.load(getClass().getResourceAsStream("MainView.fxml"));
Scene scene = new Scene(root);
stage.show();
I of course looked for solutions online but those posts talk about passing arguments between windows and such.
Thanks in advance for your help.
You can use a dynamic root in the FXML.
Briefly, the FXML will look like:
<fx:root
type="javafx.scene.layout.BorderPane"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.mycompany.myproject.MainViewController">
<!-- Controls etc. -->
</fx:root>
And then in the MainView constructor do
public class MainView extends BorderPane {
private EventBus eventBus;
public MainView(EventBus eventBus) {
this.eventBus = eventBus;
FXMLLoader loader = new FXMLLoader(getClass().getResourceAsStream("MainView.fxml"));
loader.setRoot(this);
loader.load();
}
}
The controller will just behave the same as the controller in an FXML with a static root. That is, you could communicate with the controller if needed with:
public MainView(EventBus eventBus) {
this.eventBus = eventBus;
FXMLLoader loader = new FXMLLoader(getClass().getResourceAsStream("MainView.fxml"));
loader.setRoot(this);
loader.load();
MainViewController controller = loader.getController();
// etc...
}
or if you needed to instantiate it by hand, omit the fx:controller attribute from the FXML file, and then do
public MainView(EventBus eventBus) {
this.eventBus = eventBus;
MainViewController controller = new MainViewController(...);
FXMLLoader loader = new FXMLLoader(getClass().getResourceAsStream("MainView.fxml"));
loader.setRoot(this);
loader.setController(controller);
loader.load();
}
I have 2 Controllers and I want to pass values from second controller to first controller, here are code sample:
FXMLController.java
private int paramAnswers;
private int paramNotificationTime;
private int paramNotificationDelay;
#FXML
private void handleMenuItemOptionsAction(ActionEvent event) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Options.fxml")); // UNDECORATED*
Scene scene = new Scene(root, Color.TRANSPARENT);
final Stage stage = new Stage();
stage.setTitle("Options");
stage.setScene(scene);
stage.show();
}
public void setOptionsParams(int paramAnswers, int paramNotificationTime, int paramNotificationDelay){
this.paramAnswers = paramAnswers;
this.paramNotificationTime = paramNotificationTime;
this.paramNotificationDelay = paramNotificationDelay;
}
and second controller:
OptionsController.java
private FXMLController parentController;
private int paramAnswers;
private int paramNotificationTime;
private int paramNotificationDelay;
#Override
public void initialize(URL location, ResourceBundle resources) {
.... }
#FXML
private void handleButtonSaveAction(ActionEvent event) throws IOException{
/*Pass these parameteres OptionsController parameters back to the FXMLController like parentController.setOptionsParams(paramAnswers, paramNotificationTime, paramNotificationDelay);
*/
(((Button)event.getSource()).getScene().getWindow())).close();
}
Arleady tried with parsing FXMLControler as .this into OptionsController initialize method, tried making listers and bunch of other resolved problems on stackoverflow but it just don't want work :< I need to pass that atributes back to FXMLController and close child window, so my main app would change behavior depending on passed values... :X
For any help I will be grateful
you can have a function in the second controller let's say passParams() set it's parameters what every you want to pass to that controller and from the first controller when you click on a button or something
this line
FXMLLoader.load(getClass().getResource("/fxml/Options.fxml"));
need to be changed to
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Options.fxml"));
Parent root = (Parent) loader.load();
OptionsController controller = loader.getController();
EDIT
you need to pass the first controller to the second controller
controller.setParentController(this); // this is the first controller
in Second controller
FirstController mController;
public void setParams(FirstController controller) {
this.mController = controller;
}
now in the button click function you use the mController you got from the previous step
mController.setOptionsParams(...); //send the params collected from the textfields
this function is implemented in the FirstController
Note: a more general way to do this by using call-backs it's the same but your code depends on interfaces not concrete classes by implementing a general interface in FirstController that have the setOptionParams() method
First you add a callback interface to your parent controller class:
public interface OptionCallback {
public void setOptionsParams(int paramAnswers, int paramNotificationTime, int paramNotificationDelay);
}
public class YourParentController implements Initializable, OptionCallback {
...
}
Using the FXMLLoader object you can get a hold of your child controller object and pass the parent object to it:
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("yourFXML.fxml"));
Parent root = (Parent) loader.load();
YourChildController yourChildController = loader.<YourChildController>getController();
yourChildConrtoller.registerCallback(this);
In the childController you save that callback:
private OptionCallback optionCallback;
public void registerCallback(OptionCallback callback) {
optionCallback = callback;
}
And whenever results are ready you use it to pass it to parent:
optionCallback.setOptionsParams(...);
I have a root layout with multiples Tabs. From my main App I open the root layout. There I included multiple FXMLs with their own controllers.
I am trying to pass the main controller to one of the Tabes controller.
The issue I am having, everything works as expected, but I get a null exception when I try to click on an action button from the new tab.
RootLayout FXML
<fx:indlue fx:id="myNewTabAnchorPane" source="NewTabFXML.fxml"/>
RootLayout Controller
#FXML NewTabController newTabController;
mainTabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>(){
#override
public void change(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue){
if(newValue == myTab){
newTabController.setMyRootController(this);
}
NewTabController
public void setMyRootController(RootController rootController){
this.rootController = rootController;
System.out.println(rootController.getID); // this prints fine
}
However, if I trigger this action I get blank from the same controller
#FXML
public void createAction(ActionEvent event) throws IOException{
System.out.println(rootController.getID); // with this I get null value.
}
What am I missing?
Here is the problem:
#FXML NewTabController newTabController;
It should be myNewTabAnchorPaneController which is not a partially lowercased class name, but fx:id + Controller concatenation.
I got a javafx application and I got a main where I set up my stage and launch the application. I also have a controller class:
public class Controller
{
#FXML Button button;
public void test(){
button.setText("Button");
}
}
How would I go about running the test method on startup. I know I can create an instance in the main class...
public class Main extends Application{
public void start(Stage primaryStage) throws Exception {
...
public static void main (String[] args){
launch (Main.class);
Controller cont = Controller();
cont.test();
}
}
and that would work. However it is not ideal for me. I was wondering if it was possible to run the method directly from the controller class, if not is there a better way of handling this? Thanks
The initialize() method is called automatically when the FXML is loaded:
public class Controller
{
#FXML Button button;
public void initialize(){
button.setText("Button");
}
}
Note that your code in the Main class won't work at all. First, launch() does not exit until you exit the application, and second, you are calling it on a new instance of the controller, not the one that is connected to the UI you load from the FXML file.
In javafx there are onclickaction , ondragaction, etc
But I cant find something like onstartupaction that perform an action when the application starts
See the Application javadoc for an instance of the application lifecycle. When your application starts, it's start method will be called.
Initialization of fxml is different from application startup, as one application may have many fxml documents loaded many times, each time having a new controller instantiatd and it's initialization method called. This is described in the #FXML Controllers section of the Introduction to FXML documentation.
In the following controller, the initialize method will be invoked by the FXMLLoader. Every time it loads an FXML document referencing the controller class, the loader will create a new controller instance and invoke initialize on it.
public class MyController implements Initializable {
#FXML private Button button;
#FXML
protected void initialize()
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("You clicked me!");
}
});
}
}