How to set a tool tip for a choice box in JavaFX?


Choice box

In JavaFX a choice box is represented by the class javafx.scene.control.ChoiceBox<T>. You can create a choice box by instantiating this class. A choice box holds a small set of multiple choices and, allows you to select only one of them.

It has two states −

  • Showing − You can see the list of choices in a choice box.

  • Not Showing − You can see the current choice of the choice box

Tooltip

Whenever you hover over the mouse pointer over an element (say, button, label etc..) in your application, the tool tip displays a hint about it. In JavaFX the tooltip is represented by the javafx.scene.control.Tooltip class, you can create a tooltip by instantiating it.

While instantiating the class you need to pass the text to be prompted as a parameter to its constructor (or set it using the setText() method.)

Adding Tooltip to the choice box 

You can add a tooltip to a choice box using −

  • The Install() method − This is a method of the ToolTip class, it accepts a Node and Tooltip objects as parameters. Using this you can install a tooltip on the specified node.

  • The setToolTip() method − This is a method of the javafx.scene.control.Control class ( parent class for all user interface controls.) You can also set a tooltip to a control element using the setTooltip() method.

Example

Following JavaFX example creates a choice box and adds a tooltip to it −

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class ChoiceBoxAddingTooltip extends Application {
   public void start(Stage stage) {
      //Setting the label
      Label label = new Label("Select Display Language:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Creating a choice box
      ChoiceBox choiceBox = new ChoiceBox();
      choiceBox.setValue("English");
      //Adding the choices to the observable list
      choiceBox.getItems().addAll("English", "Hindi", "Telugu", "Tamil");
      //Creating a tool tip
      Tooltip toolTip = new Tooltip("Select a Language");
      //Adding tool tip to the choice box
      choiceBox.setTooltip(toolTip);
      //Creating a HBox
      HBox box = new HBox(5);
      box.setPadding(new Insets(25, 5 , 5, 50));
      box.getChildren().addAll(label, choiceBox );
      //Setting the stage
      Scene scene = new Scene(box, 595, 170, Color.BEIGE);
      stage.setTitle("Choice Box Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 18-May-2020

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements