2

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.

enter image description here

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) {
    }

}    

enter image description here

Amita Patil
  • 1,310
  • 2
  • 14
  • 22

1 Answers1

2

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);
DVarga
  • 21,311
  • 6
  • 55
  • 60
  • What is "not working"? If you replace "..." with something valid it is working. – DVarga Jun 28 '16 at 09:09
  • when i add this code to mine, my form doesn't load properly – Amita Patil Jun 28 '16 at 09:16
  • I don't see you code. I don't know what you have done. The snippet that I posted shows how to add a Tooltip to a Button that displays a picture. It is a working snippet. If you want further help, update the question with your current code. – DVarga Jun 28 '16 at 09:20
  • // SHOW IMAGE TOOLTIP - 28 JUNE 2016 private Tooltip funShowToolTip(String imgUrl){ Image image = new Image(imgUrl, 150, 150, true, true); Tooltip tooltip = new Tooltip(); tooltip.setGraphic(new ImageView(image)); return tooltip; } // ASSIGN TOOLTIP TO BUTTONS private void funAssignToolTip() { btn_PC_OK.setTooltip(funShowToolTip("C:\\Users\\pc1\\Pictures\\DCPL\\clock.png")); } – Amita Patil Jun 28 '16 at 09:21
  • this is what i did. And I call that second function funAssignToolTip() in initialize() – Amita Patil Jun 28 '16 at 09:21
  • I meant to update the question ... nevertheless: `"C:\\Users\\pc1\\Pictures\\DCPL\\clock.png"` is not a valid URL. It shall be `"file:C:\\Users\\pc1\\Pictures\\DCPL\\clock.png"`. – DVarga Jun 28 '16 at 09:27