JavaFX example to add a tooltip to a radio button

JavaFXObject Oriented ProgrammingProgramming

Radio Button

A radio button is a type of button, which is circular in shape. It has two states, selected and deselected. Generally, radio buttons are grouped using toggle groups, where you can only select one of them. You can create a radio button in JavaFX by instantiating the javafx.scene.control.RadioButton class.

Tooltip

Whenever you hover over the mouse pointer over an element (say, button, label, etc..) in your application, the tooltip 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.)

Example

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Tooltip_ApplicationData extends Application {
   public void start(Stage stage) {
      //Creating a label
      Label label = new Label("Select Fries for Burger Meal:");
      label.setFont(new Font("Britannic Bold", 15));
      //Creating the Radio buttons
      RadioButton rb1 = new RadioButton("Regular Fries");
      RadioButton rb2 = new RadioButton("King Fries");
      RadioButton rb3 = new RadioButton("Medium Peri Peri Fries");
      RadioButton rb4 = new RadioButton("Creamy Italian Fries");
      //Adding the buttons to the toggle group
      ToggleGroup group = new ToggleGroup();
      group.getToggles().addAll(rb1, rb2, rb3, rb4);
      //Creating tool tips
      Tooltip toolTip1 = new Tooltip("70 ₹");
      Tooltip toolTip2 = new Tooltip("90 ₹");
      Tooltip toolTip3 = new Tooltip("100 ₹");
      Tooltip toolTip4 = new Tooltip("120 ₹");
      //Adding tool tips to the radio buttons
      rb1.setTooltip(toolTip1);
      rb2.setTooltip(toolTip2);
      rb3.setTooltip(toolTip3);
      rb4.setTooltip(toolTip4);
      //Adding the toggle button to the pane
      VBox vBox = new VBox(10);
      vBox.setPadding(new Insets(15, 5, 5, 100));
      vBox.getChildren().addAll(label, rb1, rb2, rb3, rb4 );
      //Setting the stage
      Scene scene = new Scene(new Group(vBox), 595, 170, Color.BEIGE);
      stage.setTitle("Tooltip Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 20-May-2020 10:22:54
Advertisements