3

I'm new the JavaFX and having some issues.

Let's say I have two fxml files, with a corresponding controller class. Each of the fxml has a button on it, which should open the other screen and pass a parameter.

Could someone please provide an example of how this is done, google hasn't been of any help.

  • Take a look at http://stackoverflow.com/questions/14915544/switching-between-windows-in-javafx-2-2/ – Sergey Grinev Oct 16 '15 at 09:59
  • I'm guessing your google research turned up: [Passing Parameters JavaFX FXML](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml), but you were unable to work out what to do from that. – jewelsea Oct 16 '15 at 21:14

2 Answers2

1

By "screens" you mean JavaFX Stage instances, right? If so, it is rather simple:

  1. Load the 2nd fxml file
  2. Acquire the controller instance via the FXML loader
  3. Pass the "parameter" by calling a setter method on the controller.

The only somewhat unusual thing is the acquiring of the controller reference. You need to create an instance of FXMLoader. It does not work with the usually called static methods:

(Main Class of the app)

public class MyFXMLApp extends Application {

  @Override
  public void start(Stage stage) throws Exception {

    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("MainForm.fxml"));

    Parent root = (Parent) loader.load();

    // as soon as the load() method has been invoked, the scene graph and the 
    // controller instance are availlable:
    MainFormController controller = loader.getController();
    controller.setText("Ready.");

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

  // ...

(The controller)

  public class MainFormController implements Initializable {

    // some ui control:
    @FXML
    private Label label;

    // JavaFX property for values that shall be accessible from outside:
    private final StringProperty text = new SimpleStringProperty();

    public String getText() {
      return text.get();
    }

    public void setText(String value) {
      text.set(value);
    }

    public StringProperty textProperty() {
      return text;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
      System.out.println("MainFormController.initialize");
      this.label.textProperty().bind(this.text);
    }

  // ...

The example uses JavaFX properties for holding the "parameter" in the controller - thus, the value and it's change are easily observable and the property may be bound to any other string property.

Robert Rohm
  • 331
  • 1
  • 7
  • This only has one screen (MainForm.fxml). Could u add a second screen (another fxml) and open it (e.g. after clicking a button). That's where I'm stuck. –  Oct 19 '15 at 08:54
  • Sorry for coming back lately - I'm over-busy at present ... I'll paste some code later, but: If you want to launch a new Window, it's more or less the code from the start()-Method: Put it into an action handler method for the button and create a new Stage instance instead of the one that gets injected with the stage parameter. – Robert Rohm Oct 21 '15 at 20:21
1
@FXML
public void handleAddPartAction(ActionEvent event) throws IOException {
        Stage stage; 
     Parent root;

        //get reference to the button's stage         
        stage=(Stage) partAddButton.getScene().getWindow();
        //load up OTHER FXML document
  root = FXMLLoader.load(getClass().getResource("AddPart.fxml"));
  Scene scene = new Scene(root);
      stage.setScene(scene);
      stage.show();