jinnings jinnings - 4 months ago 197
Java Question

how to add tooltip(image )in javafx

How to add tooltip (i button in image) in javafx.enter image description here

final Label response = new Label();
final ImageView imageViews = new ImageView(
new Image("http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Pizza-icon.png"));
final Button buttons = new Button("test", imageViews);
buttons.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
response.setText("test");
}
});

buttons.getStyleClass().add("button1");

Answer

This is code for a Tooltip with an Image:

Image image = new Image(IMAGE_LOC);
ImageView imageView = new ImageView(image);
Tooltip tooltip = new Tooltip();
tooltip.setGraphic(imageView);
button.setTooltip(tooltip);

Though, in looking at your image mockup, the highlighted areas don't really look like a Tooltip at all. It seems like what you want instead is an image or button that somebody could click on to get more information. You can do that using a standard button with a graphic in it:

Image image = new Image(IMAGE_LOC);
ImageView imageView = new ImageView(image);
Button button = new Button();
button.setGraphic(imageView);
button.setOnAction(event -> showInfoPopup());

You can style the resultant button using CSS to get the style you wish. The showInfoPopup() method in the above code is some code which you write to show the information message you wish.

Or you can just create a clickable ImageView:

Image image = new Image(IMAGE_LOC);
ImageView imageView = new ImageView(image);
Glow glow = new Glow();
imageView.effectProperty().bind(
    Bindings.when(imageView.hoverProperty()).then(glow).otherwise(null)
);
imageView.setOnMouseClicked(event -> showInfoPopup());
Comments