我正在寻找一种基于某些全局属性动态控制JavaFX应用程序中的工具提示显示的方法。假设在我的应用程序中有1000个节点安装了工具提示。我有一个布尔属性来设置工具提示打开/关闭。我怎样才能以更可行的方式实现这一目标?只是为了让您知道,我知道工具提示安装/卸载功能。每次用户切换设置属性时,我不想对所有1000个节点执行此操作。
我最初的想法是扩展Tooltip控件并为“onShowing”设置一些事件过滤器,如果setting属性为false,则会使用该事件。但这并不是要停止显示工具提示。即使我使用了该事件,也会显示工具提示。
或者,我也尝试设置事件调度程序。但那也没有奏效。下面是我尝试使用的代码。
对此有任何帮助非常感谢。
import javafx.event.Event;
import javafx.event.EventDispatcher;
import javafx.scene.control.Tooltip;
import javafx.stage.WindowEvent;
public class CustomTooltip extends Tooltip {
public static boolean SHOW_TOOLTIP = false;
public CustomTooltip(String txt){
super(txt);
// Approach #1
addEventFilter(WindowEvent.WINDOW_SHOWING, e->{
if (!SHOW_TOOLTIP) {
e.consume();
}
});
// Approach #2
//setEventDispatcher();
}
private void setEventDispatcher() {
final EventDispatcher oed = getEventDispatcher();
final EventDispatcher ned = (event, tail) -> {
Event r = null;
if (!SHOW_TOOLTIP) {
event.consume();
} else{
r = oed.dispatchEvent(event, tail);
}
return r;
};
setEventDispatcher(ned);
}
}
以下是演示我的要求的完整示例/演示。
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventDispatcher;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class TooltipDisableDemo extends Application {
public static boolean SHOW_TOOLTIP = false;
@Override
public void start(Stage stage) throws Exception {
StackPane root = new StackPane();
Scene sc = new Scene(root, 700, 700);
stage.setScene(sc);
stage.setTitle("Tooltips Disable Demo");
stage.show();
CheckBox showToolTip = new CheckBox("Enable tooltip displaying");
showToolTip.selectedProperty().addListener((obs, old, show) -> SHOW_TOOLTIP = show);
showToolTip.setSelected(true);
TabPane tabPane = new TabPane();
for (int i = 1; i < 11; i++) {
Tab tab = new Tab("Tab " + i);
tab.setClosable(false);
FlowPane fp = new FlowPane();
fp.setPadding(new Insets(10));
fp.setHgap(15);
fp.setVgap(15);
for (int j = 1; j < 51; j++) {
StackPane sp = new StackPane();
sp.setStyle("-fx-background-color: gray");
sp.setPadding(new Insets(0,5,0,5));
sp.getChildren().add(new Label("SP T"+i+" - "+j));
Tooltip.install(sp, new CustomTooltip("This is stack pane " + j + " in Tab " + i));
Button btn = new Button("Button T"+i+" - "+j);
btn.setTooltip(new CustomTooltip("This is button " + j + " in Tab " + i));
fp.getChildren().addAll(sp, btn);
}
tab.setContent(fp);
tabPane.getTabs().add(tab);
}
VBox.setVgrow(tabPane, Priority.ALWAYS);
VBox vb = new VBox();
vb.setSpacing(10);
vb.setPadding(new Insets(10));
vb.getChildren().addAll(showToolTip,tabPane);
root.getChildren().add(vb);
}
public static void main(String[] args) {
Application.launch(args);
}
/**
* Custom tooltip implementation.
*/
class CustomTooltip extends Tooltip {
public CustomTooltip(String txt){
super(txt);
// Approach #1
addEventFilter(WindowEvent.WINDOW_SHOWING, e->{
if (!SHOW_TOOLTIP) {
e.consume();
}
});
// Approach #2
//setEventDispatcher();
}
private void setEventDispatcher() {
final EventDispatcher oed = getEventDispatcher();
final EventDispatcher ned = (event, tail) -> {
Event r = null;
if (!SHOW_TOOLTIP) {
event.consume();
} else{
r = oed.dispatchEvent(event, tail);
}
return r;
};
setEventDispatcher(ned);
}
}
}
如果要在控件上安装Tooltip
,你总是可以进行绑定(例如,你不能将它用于Pane
)。
public final class ToolTipManager {
public ToolTipManager INSTANCE; // Some singleton implementation
public final ReadOnlyBooleanProperty enabledProperty() { return enabled.getReadOnlyProperty(); }
// ...
}
public class MyController {
@FXML private Button button;
private Tooltip myTooltip;
@FXML private void initialize() {
button.tooltipProperty().bind(
Bindings.when(ToolTipManager.INSTANCE.enabledProperty())
.then(myTooltip)
.otherwise((Tooltip)null));
}
}
以下是按照kleopatra的建议进行的最终工作演示。我重写了受保护的方法show()并有条件地调用super方法来显示工具提示。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TooltipDisableDemo extends Application {
public static boolean SHOW_TOOLTIP = false;
@Override
public void start(Stage stage) throws Exception {
StackPane root = new StackPane();
Scene sc = new Scene(root, 700, 700);
stage.setScene(sc);
stage.setTitle("Tooltips Disable Demo");
stage.show();
CheckBox showToolTip = new CheckBox("Enable tooltip displaying");
showToolTip.selectedProperty().addListener((obs, old, show) -> SHOW_TOOLTIP = show);
showToolTip.setSelected(true);
TabPane tabPane = new TabPane();
for (int i = 1; i < 11; i++) {
Tab tab = new Tab("Tab " + i);
tab.setClosable(false);
FlowPane fp = new FlowPane();
fp.setPadding(new Insets(10));
fp.setHgap(15);
fp.setVgap(15);
for (int j = 1; j < 51; j++) {
StackPane sp = new StackPane();
sp.setStyle("-fx-background-color: gray");
sp.setPadding(new Insets(0, 5, 0, 5));
sp.getChildren().add(new Label("SP T" + i + " - " + j));
Tooltip.install(sp, new CustomTooltip("This is stack pane " + j + " in Tab " + i));
Button btn = new Button("Button T" + i + " - " + j);
btn.setTooltip(new CustomTooltip("This is button " + j + " in Tab " + i));
fp.getChildren().addAll(sp, btn);
}
tab.setContent(fp);
tabPane.getTabs().add(tab);
}
VBox.setVgrow(tabPane, Priority.ALWAYS);
VBox vb = new VBox();
vb.setSpacing(10);
vb.setPadding(new Insets(10));
vb.getChildren().addAll(showToolTip, tabPane);
root.getChildren().add(vb);
}
public static void main(String[] args) {
Application.launch(args);
}
/**
* Custom tooltip implementation.
*/
class CustomTooltip extends Tooltip {
public CustomTooltip() {
super();
}
public CustomTooltip(String txt) {
super(txt);
}
@Override
protected void show() {
if (SHOW_TOOLTIP) {
super.show();
}
}
}
}