Java 在场景渲染前初始化中调用查找
我想使用下面的代码来同步滚动条Java 在场景渲染前初始化中调用查找,java,javafx,Java,Javafx,我想使用下面的代码来同步滚动条 Node n1 = listView1.lookup(".scroll-bar"); if (n1 instanceof ScrollBar) { final ScrollBar bar1 = (ScrollBar) n1; Node n2 = listView2.lookup(".scroll-bar"); if (n2 instanceof ScrollBar) { final ScrollBar bar2 = (Scr
Node n1 = listView1.lookup(".scroll-bar");
if (n1 instanceof ScrollBar) {
final ScrollBar bar1 = (ScrollBar) n1;
Node n2 = listView2.lookup(".scroll-bar");
if (n2 instanceof ScrollBar) {
final ScrollBar bar2 = (ScrollBar) n2;
bar1.valueProperty().bindBidirectional(bar2.valueProperty());
}
}
我想在场景的Initialize()
方法中运行这些代码行。
不幸的是,这些代码行要求场景已渲染。
有没有一种方法可以在没有任何用户输入的情况下渲染场景后运行这些行?这种难看的方法似乎奏效了:
public class Controller {
@FXML
private ListView<String> listView1 ;
@FXML
private ListView<String> listView2 ;
@FXML
private Parent root ;
public void initialize() {
root.sceneProperty().addListener((obs, oldScene, newScene) -> {
if (newScene != null) {
root.applyCss();
Node n1 = listView1.lookup(".scroll-bar");
System.out.println(n1);
if (n1 instanceof ScrollBar) {
final ScrollBar bar1 = (ScrollBar) n1;
Node n2 = listView2.lookup(".scroll-bar");
System.out.println(n2);
if (n2 instanceof ScrollBar) {
final ScrollBar bar2 = (ScrollBar) n2;
bar1.valueProperty().bindBidirectional(bar2.valueProperty());
}
}
}
});
}
}
公共类控制器{
@FXML
私有列表视图列表视图1;
@FXML
私有列表视图列表视图2;
@FXML
私有父根;
公共无效初始化(){
root.sceneProperty().addListener((obs、旧场景、新场景)->{
if(newScene!=null){
root.applyCss();
节点n1=listView1.lookup(“.滚动条”);
系统输出打印ln(n1);
if(滚动条的n1实例){
最终滚动条1=(滚动条)n1;
节点n2=listView2.lookup(“.scroll bar”);
系统输出打印ln(n2);
if(滚动条的n2实例){
最终滚动条2=(滚动条)n2;
bar1.valueProperty().bindBidirective(bar2.valueProperty());
}
}
}
});
}
}