BURNS
BURNS

Reputation: 701

Tooltip isn't being displayed on ScrollPane

Following the tutorial here I tried to create a Tooltip on a ScrollPane using the following code:

final ScrollPane scroll = new ScrollPane();

scroll.addEventHandler(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent t) {
            pointer = MouseInfo.getPointerInfo();
            point = pointer.getLocation();
            color = robot.getPixelColor((int) point.getX(), (int) point.getY());
            Tooltip tooltip = new Tooltip();
            tooltip.setText(" " + color);
            tooltip.activatedProperty();
            scroll.setTooltip(tooltip);

            System.out.println("Color at: " + point.getX() + "," + point.getY() + " is: " + color);
        }

    });

The tooltip however refuses to show itself on the ScrollPane but the output of "Color at: ..." is being printed so I am sure that handle is being called.

EDIT : On the suggestion of jewelsea , I tried putting the eventHandler on the content ,rather than the pane, but to no effect.

Upvotes: 1

Views: 640

Answers (1)

James_D
James_D

Reputation: 209714

If I understand what you're trying to do, you really only need to install the tooltip once, and then just modify its text as the mouse moves.

This works for me:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ImageTooltipTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Image image = new Image("http://www.publicdomainpictures.net/pictures/30000/velka/tropical-paradise.jpg");
        final ImageView imageView = new ImageView();
        imageView.setImage(image);
        final ScrollPane scroller = new ScrollPane();
        scroller.setContent(imageView);

        final Tooltip tooltip = new Tooltip();
        scroller.setTooltip(tooltip);

        scroller.getContent().addEventHandler(MouseEvent.MOUSE_MOVED, event -> {
            Image snapshot = scroller.getContent().snapshot(null, null);
            int x = (int) event.getX();
            int y = (int) event.getY();
            PixelReader pixelReader = snapshot.getPixelReader();
            Color color = pixelReader.getColor(x, y);
            String text = String.format("Red: %.2f%nGreen: %.2f%nBlue: %.2f", 
                    color.getRed(),
                    color.getGreen(),
                    color.getBlue());
            tooltip.setText(text);
        });



        root.setCenter(scroller);
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 2

Related Questions