gpt4 book ai didi

javafx - 如何单独引用 RangeSlider 的每个拇指 (ControlsFX)

转载 作者:行者123 更新时间:2023-12-02 01:49:36 24 4
gpt4 key购买 nike

我正在尝试绑定(bind)一个标签,使其显示在 RangeSlider 的下拇指和上拇指上方。

无论用户将其滑动到何处,标签的位置应始终保持在各自拇指的上方。就像这样:

enter image description here

enter image description here

我的方法是将监听器附加到每个拇指,以便我可以在用户每次滑动时将标签设置为具有适当的 X/Y 坐标。但是当我运行以下代码时,我似乎无法通过 css 选择器获得对个人拇指的引用。

我关注了this post ,但这仅使用一根拇指,因此很容易引用。您如何在我的上下文中正确使用 CSS 选择器,或者如果我的方法有缺陷,更好的方法是什么?

Controller

public class SliderDemoController implements Initializable {
@FXML
private RangeSlider range;
@Override
public void initialize(URL location, ResourceBundle resources) {
Pane thumb = (Pane) range.lookup(".range-slider .low-thumb");
System.out.println(thumb); // <-- Prints null
}
}

主要

public class SliderDemoMain extends Application {
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage initStage) throws IOException {
FXMLLoader loader = new FXMLLoader(Drag.class.getClassLoader().getResource("sliderdemo.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
Stage primaryStage = new Stage();
primaryStage.setTitle("Slider Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
}

styleMain.css

.range-slider .range-bar {
-fx-background-color: grey;
}

.range-slider .low-thumb {
//....
}
.range-slider .high-thumb {
//....
}

sliderdemo.fxml 的顶行

<HBox fx:id="menuBar" maxHeight="-Infinity" minHeight="-Infinity" prefHeight="79.0" stylesheets="@styleMain.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.propertydrop.SliderDemoController">

最佳答案

要使 CSS 查找发挥作用,您需要应用 CSS、创建布局 channel 以及将节点作为场景的一部分。 (请注意您引用的帖子中的这一点,答案中的代码在执行查找之前仔细执行这些步骤。)

这在 Controller 中很难实现,因为通常在将 FXML 的根节点添加到场景之前调用 initialize() 方法。您可以通过观察节点的 sceneProperty() 来破解此问题,并在其设置为非空时进行响应。 (是的,这有点黑客……):

public class SliderDemoController implements Initializable {
@FXML
private RangeSlider range;
@Override
public void initialize(URL location, ResourceBundle resources) {
ChangeListener<Scene> initializer = new ChangeListener<Scene>() {
@Override
public void changed(ObservableValue<? extends Scene> obs, Scene oldScene, Scene newScene) {
if (newScene != null) {
range.applyCss();
range.getParent().layout();
Pane thumb = (Pane) range.lookup(".range-slider .low-thumb");
System.out.println(thumb); // <-- No longer null
range.sceneProperty().removeListener(this);
}
}
};
range.sceneProperty().addListener(initializer);
}
}

根据您的具体要求,您也许可以采用一些不那么难看的方法,例如在事件处理程序中或在 slider 的 highValue 上的监听器中执行查找lowValue 属性。

关于javafx - 如何单独引用 RangeSlider 的每个拇指 (ControlsFX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41497732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com