Tool Tip not showing on mouse enter

1.3k Views Asked by At

I am trying to show tool tip on MouseEnter event on button but it's not showing. I don't understand whats wrong with my code.

Here is my fxml file in which i use button and add MouseEnter Event.

<ToolBar fx:id="logViewerToolBar" layoutX="66.0" layoutY="9.0" opacity="1.0" prefWidth="148.0">
                              <items>
                                <Button id="loadlogearlierbtn" fx:id="loadLogEarlierBtn" mnemonicParsing="false" mouseTransparent="true" onMouseEntered="#loadLogEarlierMouseEntered" onMouseExited="#loadLogEarlierMouseExited" prefWidth="35.0" styleClass="imgbtn" text="">
                                  <stylesheets>
                                    <URL value="@main.css" />
                                  </stylesheets>
                                </Button>
                                <Button id="loadlogtadaybtn" fx:id="loadLogTodayBtn" mnemonicParsing="false" onMouseEntered="#loadLogTodayMouseEntered" onMouseExited="#loadLogTodayMouseExited" prefWidth="35.0" styleClass="imgbtn" text="">
                                  <stylesheets>
                                    <URL value="@main.css" />
                                  </stylesheets>
                                </Button>
                                <Button id="searchlogbtn" fx:id="btnFind" mnemonicParsing="false" onMouseEntered="#findLogMouseEntered" onMouseExited="#findLogMouseExited" prefWidth="35.0" styleClass="imgbtn" text="">
                                  <stylesheets>
                                    <URL value="@main.css" />
                                  </stylesheets>
                                </Button>
                              </items>
                            </ToolBar>  

Here is my controller class.

@FXML
public void findLogMouseEntered(MouseEvent event)
{      
    btnFind.setTooltip(new Tooltip("Search field value in to entire log"));
}

But it's still not showing tooltips. And definitely, This is really easy question but i newbie on javaFx.

I also link button like

@FXML
private Button loadLogEarlierBtn,loadLogTodayBtn,btnFind;     

Even following way not working.

<Button id="searchlogbtn" fx:id="btnFind" mnemonicParsing="false" prefWidth="35.0" styleClass="imgbtn" text="">
                                  <tooltip>
                                        <Tooltip text="Search field value in to entire log"/>
                                    </tooltip>
                                    <stylesheets>
                                    <URL value="@main.css" />
                                  </stylesheets>

                                </Button>

Please give me hint or reference.

2

There are 2 best solutions below

0
On

not really sure but i think adding the title="here goes the title" should do the trick.

hope this helps

0
On

Either put the setTooltip(...) in the initialize(...) method, or initialize the tooltip in FXML.

Edit: Here's a complete example:

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ButtonWithTooltipExample extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        Scene scene = new Scene(FXMLLoader.<Parent>load(getClass().getResource("ButtonWithTooltip.fxml")), 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

ButtonWithTooltip.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Tooltip?>

<BorderPane xmlns:fx="http://javafx.com/fxml">
    <center>
        <Button text="Click Me">
            <tooltip>
                <Tooltip text="This is a button. Click on it."/>
            </tooltip>
        </Button>
    </center>
</BorderPane>

You don't need the onMouseEntered or onMouseExited - the tooltip takes care of this.