How to show Image as tooltip in JavaFX?

不想你离开。 提交于 2019-12-10 11:09:13

问题


I would like to show an image as a tooltip. It works OK but at some random points it shows fluctuation. I want to show it normally without getting fluctuate.

I show a new scene (in which i added my image-view with image) on mouse enter event and close it on mouse leave event event

//  MOUSE ENTER PHOTO CORRECTIO
@FXML
private void mouseEnterPhotoCorrection(MouseEvent event) {
    if (f_ShowToolTip) {
        Stage stg = funShowImageTooltip();
        double x, y;
        x = event.getScreenX();
        y = event.getScreenY();
        stg.setX(x);
        stg.setY(y);
        stg.show();
        f_ShowToolTip = false;
    }
}

//  MOUSE LEAVE PHOTO CORRECTIO
@FXML
private void mouseLeavePhotoCorrection(MouseEvent event) {
    funHideImageTooltip();
    f_ShowToolTip = true;
}

/****************************** FUNCTIONS *******************************/

Stage s;
boolean f_ShowToolTip;

//  FUNCTION TO SET INITAL STATE OF PHOTOS AND CORRECTION
private void funInitPhotosCorrection()
{
    f_ShowToolTip = true;
}

private Stage funShowImageTooltip()
{
    try {
        s = new Stage();

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("frmImageToolTip.fxml"));

        Parent root = (Parent) fxmlLoader.load();
        Scene scene = new Scene(root);                

        s.setScene(scene);
        s.setResizable(false);

        s.initModality(Modality.WINDOW_MODAL);
        s.initStyle(StageStyle.UNDECORATED);

        s.setResizable(false);

        double x, y;
        //x = btn_Red.

        s.show();

        }catch(Exception e1)
        {
        }
    return s;
}

private void funHideImageTooltip()
{
    try {
        s.close();
    } catch (Exception e) {
    }

}    


回答1:


If you just simply want to have a tooltip over a Node (a Button in your case), it is reasonable to use a Tooltip and its graphicProperty rather than showing a different Stage.

// Load the image with the needed size
Image image = new Image("...", 150, 150, true, true);

// Place it over a Button
Button button = new Button();

Tooltip tooltip = new Tooltip();
tooltip.setGraphic(new ImageView(image));

button.setTooltip(tooltip);


来源:https://stackoverflow.com/questions/37920558/how-to-show-image-as-tooltip-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!