Saturday, December 29, 2012

Open new window in JavaFX 2

The example demonstrate how to open new window in JavaFX 2, once button clicked.

Open new window in JavaFX 2


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testjavafxwindow;

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

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFXWindow extends Application {
    
    @Override
    public void start(final Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Open a New Window");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                
                Label secondLabel = new Label("Hello");
                
                StackPane secondaryLayout = new StackPane();
                secondaryLayout.getChildren().add(secondLabel);
                
                Scene secondScene = new Scene(secondaryLayout, 200, 100);

                Stage secondStage = new Stage();
                secondStage.setTitle("Second Stage");
                secondStage.setScene(secondScene);
                
                //Set position of second window, related to primary window.
                secondStage.setX(primaryStage.getX() + 250);
                secondStage.setY(primaryStage.getY() + 100);
 
                secondStage.show();
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


3 comments:

  1. Nice! Thanks for that! It very helpful.

    Maybe you know: how too snap* two windows?

    *like in old winamp or like panels in photoshop

    ReplyDelete
  2. And what's happen if the first window will be close? The second window will remanins open?

    ReplyDelete