Original address http://download.oracle.com/javafx/2.0/ui_controls/tooltip.htm#BABBIJBJ
The tooltip class generates a common UI control, which is generally used to add information to the UI control. Place the mouse over the control and the prompt bar will be displayed. Use of any controls
The settooltip method can be used to add a prompt bar.
The prompt bar has two statuses: active and displayed. When the mouse is placed on the control, the prompt bar is activated. When it is displayed, it is in the "displayed" status, and the displayed prompt bar is also activated. There is a delay between the activation and display of the prompt bar.
The password with a prompt is shown in Figure 18-1.
Figure 18-1 tooltip added to a password field
Description of "Figure 18-1 tooltip added to a password field"
Create tooltip
Study the code in example 18-1, which creates the above application.
Example 18-1 adding a tooltip to the password field
final PasswordField pf = new PasswordField();final Tooltip tooltip = new Tooltip();tooltip.setText( "\nYour password must be\n" + "at least 8 characters in length\n" +);pf.setTooltip(tooltip);
Each control in the javafx. Scene. Control package has
setTooltip
Method. Can define text, useTooltip constructor or
setText
Method
.
BecauseTooltip
Class inheritedLabeled
Class, so you can not only add text, but also add graphics. Example
The code block in 18-2 adds an icon to the prompt bar in the password box.
Example 18-2 adding an icon to a tooltip
Image image = new Image( getClass().getResourceAsStream("warn.png"));tooltip.setGraphic(new ImageView(image));
For the running effect, see Figure 18-2.
Figure 18-2 tooltip with an icon
Description of "Figure 18-2 tooltip with an icon"
The prompt bar not only provides auxiliary information, but also displays data.
Present data in the prompt bar
The application displayed in Figure 18-3 calculates the total cost of hotel accommodation based on the information displayed in the prompt bar.
Figure 18-3 calculating hotel rates
Description of "Figure 18-3 calculating hotel rates"
Each checkbox is accompanied by a tooltip. Each check box has a prompt, and each prompt bar shows the cost of a specific scheduled project. If you select the check box, the corresponding value is added to the total number. Of course, if you deselect this option, it will also be subtracted from the total number.
Check out the application code example 18-3.
Example 18-3 using tooltips to calculate hotel rates
import javafx.application.Application;import javafx.beans.value.ChangeListener;import javafx.beans.value.ObservableValue;import javafx.geometry.Insets;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.CheckBox;import javafx.scene.control.Label;import javafx.scene.control.Tooltip;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.scene.text.Font;import javafx.stage.Stage; public class Main extends Application { final static String[] rooms = new String[]{ "Accommodation (BB)", "Half Board", "Late Check-out", "Extra Bed" }; final static Integer[] rates = new Integer[]{ 100, 20, 10, 30 }; final CheckBox[] cbs = new CheckBox[rooms.length]; final Label total = new Label("Total: $0"); Integer sum = 0; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Tooltip Sample"); stage.setWidth(300); stage.setHeight(150); total.setFont(new Font("Arial", 20)); for (int i = 0; i < rooms.length; i++) { final CheckBox cb = cbs[i] = new CheckBox(rooms[i]); final Integer rate = rates[i]; final Tooltip tooltip = new Tooltip("$" + rates[i].toString()); tooltip.setFont(new Font("Arial", 16)); cb.setTooltip(tooltip); cb.selectedProperty().addListener(new ChangeListener<Boolean>() { public void changed(ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) { if (cb.isSelected()) { sum = sum + rate; } else { sum = sum - rate; } total.setText("Total: $" + sum.toString()); } }); } VBox vbox = new VBox(); vbox.getChildren().addAll(cbs); vbox.setSpacing(5); HBox root = new HBox(); root.getChildren().add(vbox); root.getChildren().add(total); root.setSpacing(40); root.setPadding(new Insets(20, 10, 10, 20)); ((Group) scene.getRoot()).getChildren().add(root); stage.setScene(scene); stage.show(); }}
Add the code in example 18-4 to Example
From 18 to 3, a prompt bar is created and a text is allocated.Integer of project price
The value is convertedString
Value.
Example 18-4 setting the value for a tooltip
final Tooltip tooltip = new Tooltip("$" + rates[i].toString())
You can use CSS to change its appearance.