Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 2.0 subwindow

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.

like image 747
fxuser Avatar asked Apr 03 '12 16:04

fxuser


2 Answers

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.

like image 172
Sergey Grinev Avatar answered Sep 28 '22 15:09

Sergey Grinev


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();
  }
}
like image 23
jewelsea Avatar answered Sep 28 '22 14:09

jewelsea