Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Created February 27, 2012 18:50
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jewelsea/1926196 to your computer and use it in GitHub Desktop.
Save jewelsea/1926196 to your computer and use it in GitHub Desktop.
JavaFX Popup example
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Popup;
import javafx.stage.Stage;
public class PopupExample extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(final Stage primaryStage) {
primaryStage.setTitle("Popup Example");
final Popup popup = new Popup(); popup.setX(300); popup.setY(200);
popup.getContent().addAll(new Circle(25, 25, 50, Color.AQUAMARINE));
Button show = new Button("Show");
show.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
popup.show(primaryStage);
}
});
Button hide = new Button("Hide");
hide.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
popup.hide();
}
});
HBox layout = new HBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().addAll(show, hide);
primaryStage.setScene(new Scene(layout));
primaryStage.show();
}
}
@jewelsea
Copy link
Author

@jewelsea
Copy link
Author

In general, for most common UI use cases, rather than creating a Popup control like this sample, I find it preferable to just creating a new Stage or use newer JavaFX facilities such as Alerts and Dialogs. Popups are generally of more use to library developers rather than application developers.

@parthask
Copy link

parthask commented Nov 7, 2016

I would like to have multiple inputs in a popup window with javafx. How to do so? Little bit confused. please help ma out.
Connect me @parthalusyl@gmail.com. Thanks in advance.

@Redeem-Grimm-Satoshi
Copy link

Great work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment