简体   繁体   中英

How to add Tooltips to button in an anchorpane?

I have some buttons inside an anchorpane and want to know how I can add Tooltips to a button. This is my fxml file

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="800.0" prefWidth="1855.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="calibre.FXMLDocumentController">
    <children>
        <Button fx:id="button" layoutX="6.0" layoutY="7.0" onAction="#handleAddBookButtonAction" prefHeight="32.0" prefWidth="200.0" text="Add Books" /> 
            <tooltip><Tooltip text="my tooltip" /></tooltip>
        </Button>
        <Label fx:id="label" layoutX="6.0" layoutY="7.0" minHeight="16" minWidth="69" />
      <Button layoutX="274.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleEditMetadataButtonAction" prefHeight="32.0" prefWidth="200.0" stylesheets="@style.css" text="Edit Metadata" />
      <Button layoutX="564.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleFetchNewsButtonAction" prefHeight="32.0" prefWidth="200.0" text="Fetch News" />
      <Button layoutX="839.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleRemoveButtonAction" prefHeight="32.0" prefWidth="200.0" text="Remove" />
      <Button layoutX="1225.0" layoutY="9.0" mnemonicParsing="false" onAction="#handleLibraryButtonAction" prefHeight="32.0" prefWidth="200.0" text="Library" />
      <MenuButton layoutX="1607.0" layoutY="11.0" minWidth="200.0" mnemonicParsing="false" text="MenuButton" textFill="#eeeeee">
        <items>
          <MenuItem mnemonicParsing="false" text="Action 1" />
          <MenuItem mnemonicParsing="false" text="Action 2" />
        </items>
      </MenuButton>
      <ImageView fitHeight="564.0" fitWidth="358.0" layoutX="747.0" layoutY="166.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@Books/1.png" />
         </image></ImageView>
      <ScrollBar layoutX="54.0" layoutY="773.0" prefHeight="18.0" prefWidth="1781.0" />
      <ImageView fitHeight="564.0" fitWidth="358.0" layoutX="149.0" layoutY="180.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@Books/4.jpg" />
         </image>
      </ImageView>
      <ImageView fitHeight="564.0" fitWidth="358.0" layoutX="1325.0" layoutY="159.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@Books/3.png" />
         </image>
      </ImageView>
      <Button layoutX="382.0" layoutY="722.0" mnemonicParsing="false" onAction="#handleReadHemingwayButtonAction" onDragOver="#handleReadBookHoverTip" text="Read" />
      <Button layoutX="985.0" layoutY="722.0" mnemonicParsing="false" onAction="#handleReadGentlemanButtonAction" text="Read" />
      <Button layoutX="1580.0" layoutY="731.0" mnemonicParsing="false" onAction="#handleReadFlowersButtonAction" text="Read" />
    </children>
</AnchorPane>

But it says tooltips don't exist in Anchorpanes. Does this mean I need to change from an Anchorpane, if so what are my alternatives. I'm doing this in Netbeans.

Warm Regards

if you have controller class you can get tooltip from this method inside the initialize method

yourBtnId.setTooltip(new Tooltip("Add Customer"));

If you are using XML editing

<Button>
     <tooltip><Tooltip  textAlignment="CENTER" text="HELLO" /></tooltip>
</Button>

in your XML file remove your all button tag and paste this

<Button fx:id="button" layoutX="6.0" layoutY="7.0" onAction="#handleAddBookButtonAction" prefHeight="32.0" prefWidth="200.0" text="Add Books"> 
            <tooltip><Tooltip text="my tooltip" /></tooltip>

</Button>

your mistake is Button tag is closing before you add the tooltip />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM