How to add content to PopupControl in JavaFX 2.2.6?

I want to implement a simple popup control that should be with CSS. Everything works fine, the only question is how to add content to it (a Node in JavaFX)?

The PopupWindow.getContent() method is deprecated in JavaFX 2.2.6 and does not work with CSS, I can see the content, but the CSS selector will not work.

So, what is the best solution for adding content on my own, should I implement my own Skin class for this purpose, or is there an easy way to just make it work?

I prepared a simple usage example:

 import javafx.scene.control.Label; import javafx.scene.control.PopupControl; import javafx.scene.layout.StackPane; import javafx.scene.shape.Rectangle; public class PopupTest extends PopupControl { public PopupTest() { getStyleClass().add("popup"); // not working!? StackPane pane = new StackPane(); pane.getStyleClass().add("pane"); Rectangle rectangle = new Rectangle(250, 250); rectangle.getStyleClass().add("rect"); Label text = new Label("popup test"); text.getStyleClass().add("text"); pane.getChildren().addAll(rectangle, text); // how to display to pane when the popup is shown? getContent().addAll(pane); } } 

For completeness, here are the MainApplication and CSS files:

 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.stage.Stage; public class MainApplication extends Application { @Override public void start(Stage stage) throws Exception { Group root = new Group(); final Scene scene = new Scene(root); scene.getStylesheets().add(MainApplication.class.getResource("style.css").toExternalForm()); final Button button = new Button("show popup"); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { PopupTest popup = new PopupTest(); popup.show(scene.getWindow()); } }); root.getChildren().add(button); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } 

style.css:

 .popup { -fx-font-size: 24px; } .popup .rect { -fx-fill: green; } .popup .text { -fx-text-fill: white; -fx-font-weight: bold; } 

The ".popup" selector does not work here, if I set it to the "panel", it will create a popup so that css is right: pane.getStyleClass().add("popup"); // working with this "fix" pane.getStyleClass().add("popup"); // working with this "fix" .

+4
source share
1 answer

This seems to work:

 getScene().setRoot(pane); 

The style class does not work: PopupControl does not have a getStylesheets() method. So maybe it can only be in the style of setStyle(...) ? You can get around this with a simple pane style or wrap a pane in the root panel and style it.

+2
source

All Articles