12
votes

How can I display a new window in JavaFX 2.0? For example after button click action. I want both windows (the main window and the new window) to communicate each other.

Thx for help.

3
I think there's an example in Ensemble. - assylias

3 Answers

17
votes
new Stage(new Scene(new Group(new Text(10,10, "my second window")))).show();

Communicating between two windows is similar as for any two objects in Java.

13
votes

You create new windows by calling new Stage() and show them by stage.show().

Here is an example of creating a new Stage with a checkbox control which modifies text of a label displayed in a different Stage.

import javafx.application.Application;
import javafx.beans.value.*;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.*;

public class SecondStage extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage primaryStage) {
    // setup some dymamic data to display.
    final String STANDARD_TEXT  = "Every Good Boy Deserves Fruit";
    final String ALTERNATE_TEXT = "Good Boys Deserve Fruit Always";        
    final Label label = new Label(STANDARD_TEXT);

    // configure the primary stage.
    StackPane primaryLayout = new StackPane();
    primaryLayout.getChildren().add(label);
    primaryLayout.setStyle("-fx-background-color: lightgreen; -fx-padding: 10;");
    primaryStage.setScene(new Scene(primaryLayout, 200, 100));
    primaryStage.setTitle("Primary Stage");

    // configure the secondary stage.
    final Stage secondaryStage = new Stage(StageStyle.UTILITY);
    CheckBox alternateTextCheck = new CheckBox("Show alternate text");
    alternateTextCheck.selectedProperty().addListener(new ChangeListener<Boolean>() {
      @Override public void changed(ObservableValue<? extends Boolean> selected, Boolean oldValue, Boolean newValue) {
        if (newValue) label.setText(ALTERNATE_TEXT); else label.setText(STANDARD_TEXT);
      }
    });
    StackPane secondaryLayout = new StackPane();
    secondaryLayout.getChildren().add(alternateTextCheck);
    secondaryLayout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
    secondaryStage.setScene(new Scene(secondaryLayout, 200, 100));
    secondaryStage.setTitle("Secondary Stage");

    // specify stage locations. 
    secondaryStage.setX(400); secondaryStage.setY(200);
    primaryStage.setX(400);   primaryStage.setY(350);

    // add a trigger to hide the secondary stage when the primary stage is hidden.
    // this will cause all stages to be hidden (which will cause the app to terminate).
    primaryStage.setOnHidden(new EventHandler<WindowEvent>() {
      @Override public void handle(WindowEvent onClosing) {
        secondaryStage.hide();
      }
    });

    // show both stages.
    primaryStage.show();
    secondaryStage.show();
  }
}
0
votes

Inside the button click action you can create a new satge and then a object of the other class you want to display. after that call the start method using the created object.

      Stage stage= new Stage();
      NewClass nc= new NewClass();
      nc.start(stage);

hope this will work!!!