简体   繁体   中英

JavaFx: How to install a Tooltip on ImageView

I tried this:

public void addTargetCard(MissionCard mCard) {
    int card = mCard.GetID();
    leftSide.getChildren().removeAll(targetCardBox);
    Image image = new Image(
            MainApp.class.getResourceAsStream("images/target" + card
                    + ".png"));
    ImageView imageView = new ImageView();
    imageView.setImage(image);
    imageView.setFitHeight(81);
    imageView.setFitWidth(108);
    imageView.setPreserveRatio(true);
    imageView.setPickOnBounds(true);
    Tooltip.install(imageView, new Tooltip(intToCity(mCard.getStart())
            + " - " + intToCity(mCard.getFinish())));
    targetCardBox.getChildren().add(imageView);
    leftSide.getChildren().add(targetCardBox);
}

Can somebody explain me why my Tooltip doesn't work - i got no idea what i did wrong. (It's my first time that i use Tooltips's)


somebody else told me that ImageView doesnt work with tooltips and gave me this workaround - but i have again no tooltip when i move with my mouse over the label

    public void addTargetCard(MissionCard mCard) {
    int card = mCard.GetID();
    leftSide.getChildren().removeAll(targetCardBox);
    Image image = new Image(
            MainApp.class.getResourceAsStream("images/target" + card
                    + ".png"));
    ImageView imageView = new ImageView();
    imageView.setImage(image);
    imageView.setFitHeight(81);
    imageView.setFitWidth(108);
    imageView.setPreserveRatio(true);
    imageView.setPickOnBounds(true);
    Label label = new Label();
    label.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    label.setGraphic(imageView);
    label.setTooltip(new Tooltip(intToCity(mCard.getStart()) + " - "
            + intToCity(mCard.getFinish())));
    targetCardBox.getChildren().add(label);
    leftSide.getChildren().add(targetCardBox);
}

Installing a tooltip to image view is working. Try yourself with new sample JavaFX project and see it. When you doubt about some functionality of the used API (JavaFX in this case) try to isolate the doubted use case into new fresh environment/project and observe it closely.

PS Why are you removing the targetCardBox from leftSide and adding it again afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM