/* * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. */ package helloworld; import javafx.application.Application; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Popup; import javafx.stage.Stage; /** * * @author jcambon */ public class HelloPopupJC extends Application { private static int WIDTH = 300; private static int HEIGHT = 300; private static int X = 500; private static int Y = 500; private static int nbContent = 0; private Stage stage; @Override public void start(Stage stage) { this.stage = stage; stage.setTitle("Hello Popup"); stage.setWidth(WIDTH); stage.setHeight(HEIGHT); stage.setX(X); stage.setX(Y); stage.setScene(createScene()); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } private Group createRootGroup() { Group rootGroup = new Group(); ObservableList content = rootGroup.getChildren(); Rectangle rect = new Rectangle(); rect.setWidth(WIDTH); rect.setHeight(HEIGHT); rect.setFill(Color.GRAY); content.add(rect); final Button showButton = new Button("Popup"); showButton.setLayoutX(50); showButton.setLayoutY(50); showButton.setOnAction(new PopupActionHandler(showButton)); content.add(showButton); return rootGroup; } private Scene createScene() { Scene scene = new Scene(createRootGroup()); return scene; } private static Popup createPopup() { Popup popup = new Popup(); final VBox vbox = new VBox(10); Button button = new Button("Add Content"); vbox.getChildren().add(button); button.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { nbContent++; Rectangle rect = new Rectangle(nbContent*10, nbContent*10); TextField tf = new TextField("Hello, how do you do ?"); HBox hbox = new HBox(10); hbox.getChildren().addAll(rect, tf); vbox.getChildren().add(hbox); } }); final StackPane sp = new StackPane(); sp.getChildren().add(vbox); sp.setStyle("-fx-background-color: white;"); popup.getContent().add(sp); popup.setAutoHide(true); return popup; } private final class PopupActionHandler implements EventHandler { private final Node popupParent; private final int popupX; private final int popupY; private Popup popup; public PopupActionHandler(Node popupParent) { this.popupParent = popupParent; this.popupX = X + WIDTH; this.popupY = Y + HEIGHT; } public void handle(final ActionEvent t) { if (popup == null) { popup = createPopup(); } popup.show(popupParent, popupX, popupY); } } }