Tuesday, July 30, 2013

JavaFX example: create new window at run-time

JavaFX example to create new window at run-time:

JavaFX example: create new window at run-time
JavaFX example: create new window at run-time
In action:



package javafx_newwindow;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_NewWindow extends Application {

@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("New a Window");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
final Stage newStage = new Stage();
Group newRoot = new Group();
Scene scene = new Scene(newRoot, 300, 200);
newStage.setScene(scene);
newStage.show();

VBox vBox = new VBox();

Label newLabel = new Label();
newLabel.setText(newStage.toString());

Button btnClose = new Button("Close");
btnClose.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
newStage.close();
}
});

vBox.getChildren().addAll(newLabel, btnClose);
newRoot.getChildren().add(vBox);
}
});

StackPane root = new StackPane();
root.getChildren().add(btn);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}


No comments:

Post a Comment