如何在JavaFX中显示控件的帮助弹出窗口?

时间:2017-02-04 16:17:14

标签: javafx

我使用以下代码来显示一些弹出窗口。

if(isIdDuplicated) {
    showTooltip(stage, textField_id, "This id have already been used!");
}

public static void showTooltip(Stage owner, Control control, String tooltipText) {
    Point2D p = control.localToScene(0.0, 0.0);

    final Tooltip customTooltip = new Tooltip();
    customTooltip.setText(tooltipText);

    control.setTooltip(customTooltip);

    customTooltip.show(owner, p.getX()
        + control.getScene().getX() + control.getScene().getWindow().getX(), p.getY()
        + control.getScene().getY() + 

    control.getScene().getWindow().getY());
}

以下是我必须面对的问题:当移动窗口时,上面的帮助弹出窗口(实际上是工具提示)不会随textField_id一起移动。任何人都可以告诉我在不使用工具提示的情况下执行此功能的最佳方法吗?是否有任何第三方库在JavaFX中有像控件一样的帮助弹出窗口?

以下是众所周知的帮助弹出窗口的示例: enter image description here

1 个答案:

答案 0 :(得分:0)

像这样的东西?

@Override
public void start(Stage stage) {
    stage.setTitle("My story");
    BorderPane bp = new BorderPane();
    bp.setStyle("-fx-background-image : url(\"http://thechronicle.com.gh/wp-content/uploads/2016/10/Juliet-Ibrahim.jpg\")");
    Button b = new Button("GET TO KNOW HER");
    b.setStyle("-fx-background-color : white;");
    b.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent paramT) {
            Popup p = new Popup();
            VBox hbox = new VBox(0); //it was hbox at first
            hbox.setFillWidth(true);
            hbox.setPrefWidth(400);
            hbox.setMinHeight(400);
            hbox.setPadding(new Insets(4));
            hbox.setEffect(new DropShadow());
            hbox.setStyle("-fx-background-color:white; -fx-background-radius: 5;");

            Label l = new Label("You see i went to school with this woman, private school, but i was little like at age 7, and she was like 14"
                    + "\n  see i could barely get an erection, lol, and you see we were very noisy kids, hence they seniors always came to shut us up."
                    + " see i was a very very bad 7 year old, and you know how these teen chicks they love bad kids, lol. but i was too young.");
            l.setWrapText(true);
            l.setPrefWidth(400);
            l.setStyle("-fx-background-color:white;");
            HBox.setHgrow(l, Priority.SOMETIMES);

            Label l1 = new Label("Look at her y'all, she married though, but you know crazy me, i had all these and i i fell in love with ruby and some chick name java"
                    + ", i dont know what i was thinking,"
                    + " i dont know who cursed me, lol, shit, here name is Juiliet Ibrahim. lol, thats my sister,"
                    + " you see its not very hard to replicate, i have lights out, runing on a 30 mins laptop, this demo, i will pimp it tomorrow,"
                    + " aka - show you more pics of my sister lol.");
            l1.setWrapText(true);
            l1.setPrefWidth(400);
            l1.setStyle("-fx-background-color:white; -fx-text-fill: black;");
            HBox.setHgrow(l1, Priority.SOMETIMES);

            Label l2 = new Label("THIS IS A POP UP MENU");
            l2.setWrapText(true);
            l2.setPrefWidth(400);
            l2.setStyle("-fx-background-color:blueviolet; -fx-text-fill: white;");
            l2.setFont(Font.font(Font.getFontNames().get(18), FontWeight.EXTRA_BOLD, 22));
            HBox.setHgrow(l1, Priority.SOMETIMES);

            hbox.getChildren().addAll(l,
                    new HBox(new ImageView(new Image("https://s-media-cache-ak0.pinimg.com/originals/82/57/c1/8257c1c3e05d3e5c0d8a945e9d1b8a11.jpg",133,0,true,true,true)),
                    new ImageView(new Image("https://www.bellanaija.com/wp-content/uploads/2016/03/Juliet-Ibrahim-2-600x867.jpg",133,0,true,true,true)),
                    new ImageView(new Image("http://www.gistus.com/gs-c/uploads/2015/10/juliet8.jpg",133,0,true,true,true))),
                    l1,l2);
            p.getContent().add(hbox);
            p.setAnchorX(b.getLayoutX() + b.getTranslateX() + stage.getX()+ b.prefWidth(-1) + 5);//just for demo
            p.setAnchorY(b.getLayoutY() + b.getTranslateY() + stage.getY()+ b.prefHeight(-1));//just for demo
            //for demostration sake
            Bindings.createDoubleBinding(new Callable<Double>() {

                @Override
                public Double call() throws Exception {
                    return 0.0;
                }
            }, stage.xProperty(),stage.yProperty());
            new DoubleBinding() {
                {
                    super.bind(stage.xProperty());
                    super.bind(stage.yProperty());
                }
                @Override
                protected double computeValue() {
                    return stage.xProperty().add(stage.yProperty()).get();//dont try this at work :)
                }
            }.addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> paramObservableValue, Number paramT1,
                        Number paramT2) {
                    p.setAnchorX(b.getLayoutX() + b.getTranslateX() + stage.getX()+ b.prefWidth(-1) + 5);//just for demo
                    p.setAnchorY(b.getLayoutY() + b.getTranslateY() + stage.getY()+ b.prefHeight(-1));//just for demo

                }
            });
            p.setAutoFix(true);
            p.show(stage);

        }
    });
    bp.setBottom(b);
    stage.setScene(new Scene(bp,600,500));
    stage.show();
}

enter image description here