I load the FXML1 in the start method and when the Button is clicked i open up the FXML2. Here's my question: can I change the text of the Label "label" when the button is pressed?
Main:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXMLExample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXML1.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Controller: in the initialize method is the comment: label.setText("Hello World"); If I uncomment it and try it i get an Exception.
package fxmlexample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
/**
* FXML Controller class
*/
public class FXMLController implements Initializable {
@FXML
Button button;
@FXML
Label label;
public void pressButton(ActionEvent event) throws Exception {
if (event.getSource() == button) {
Parent root = FXMLLoader.load(getClass().getResource("FXML2.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
}
/**
* Initializes the controller class.
*
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// label.setText("Hello World");
}
}
FXML1:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmlexample.FXMLController">
<children>
<Button fx:id="button" layoutX="264.0" layoutY="185.0" mnemonicParsing="false" onAction="#pressButton" prefHeight="31.0" prefWidth="73.0" text="Click me!" />
</children>
</Pane>
FXML2:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<Pane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmlexample.FXMLController">
<children>
<Label fx:id="label" layoutX="244.0" layoutY="177.0" prefHeight="46.0" prefWidth="112.0" style="-fx-background-color: lime;" />
</children>
</Pane>
FXMLLoaderor loading the fxml multiple times and trying to manipulate a previously loaded scene using the new one... Please create a minimal reproducible example. This would make it far easier to come up with a recommendatio instead of simply answering "Yes". - fabian