4

CSS でスタイル設定できる単純なポップアップ コントロールを実装したいと考えています。唯一の問題は、コンテンツ (JavaFX のノード) を追加する方法です。

このPopupWindow.getContent()メソッドは JavaFX 2.2.6 で廃止され、CSS では機能しません。コンテンツは表示できますが、CSS セレクターは機能しません。

それで、自分でコンテンツを追加するための最良のソリューションは何ですか?その目的のために自分のスキンクラスを実装する必要がありますか、それとも単に機能させる簡単な方法がありますか?

簡単なユースケースを用意しました:

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);
    }
}

完全を期すために、ここに私の MainApplication と CSS ファイルを示します。

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);
    }
}

スタイル.css:

.popup {
    -fx-font-size: 24px;
}

.popup .rect {
    -fx-fill: green;
}

.popup .text {
    -fx-text-fill: white;
    -fx-font-weight: bold;
}

「.popup」セレクターはここでは機能しません。「ペイン」に設定すると、ポップアップ ウィンドウのスタイルが設定されるため、css は正しくなりますpane.getStyleClass().add("popup"); // working with this "fix"

4

1 に答える 1