【发布时间】:2017-01-08 15:36:59
【问题描述】:
在我的应用程序中,我有一个带有 VBox 的 ScrollPane,它可能包含 0 到 1000 个窗格,每个窗格的设置高度为 90。每个窗格都有相关的项目,可以从内部的按钮加载,该按钮清除 VBox 并添加其子项以及“返回结果”按钮,该按钮加载前一个列表并应返回到 ScrollPane 中的同一位置。但是, setVvalue() 似乎没有工作。窗格及其子级属于同一类。
package application;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
public class Main extends Application {
public VBox resultsBox;
public ScrollPane scroll;
public List<CustomClass> results = new ArrayList<CustomClass>();
public Button returnToResults;
public class CustomClass extends HBox {
public List<CustomClass> items;
public boolean hasItems() {
return items != null && !items.isEmpty();
}
public CustomClass(String text, boolean hasItems) {
setMinHeight(90);
setPrefHeight(90);
setMaxHeight(90);
setMaxWidth(Double.MAX_VALUE);
setAlignment(Pos.CENTER);
setStyle("-fx-border-color: red");
Button children = new Button("View Children "+text);
children.setDisable(!hasItems);
getChildren().add(children);
children.setOnAction(e -> {
viewChildren(this);
});
if(hasItems) {
items = new ArrayList<CustomClass>();
for(int i=0; i<10; i++) {
items.add(new CustomClass(text+"."+i, false));
}
}
}
}
public double vValue = 0.0;
public void viewChildren(CustomClass cc) {
Platform.runLater(() -> {
vValue = scroll.getVvalue();
System.out.println("0. "+vValue);
resultsBox.getChildren().clear();
resultsBox.getChildren().addAll(cc.items);
returnToResults.setDisable(false);
});
}
public void returnToResults() {
Platform.runLater(() -> {
resultsBox.getChildren().clear();
resultsBox.getChildren().addAll(results);
System.out.println("1. "+vValue+" "+scroll.getVvalue());
scroll.setVvalue(vValue);
System.out.println("2. "+vValue+" -> "+scroll.getVvalue()); // Sets correctly.
returnToResults.setDisable(true);
});
}
public void start(Stage stage) throws Exception {
resultsBox = new VBox(5);
resultsBox.setFillWidth(true);
resultsBox.setAlignment(Pos.TOP_CENTER);
scroll = new ScrollPane(resultsBox);
scroll.setMaxWidth(Double.MAX_VALUE);
scroll.setFitToWidth(true);
for(int i=0; i<100; i++) {
results.add(new CustomClass("#"+i, true));
}
resultsBox.getChildren().addAll(results);
returnToResults = new Button("Return to Results");
returnToResults.setStyle("-fx-base: green");
returnToResults.setDisable(true);
returnToResults.setOnAction(e -> {
returnToResults();
});
BorderPane root = new BorderPane();
root.setCenter(scroll);
root.setBottom(returnToResults);
Scene scene = new Scene(root,400,400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
setVvalue() 工作并将 ScrollPane 打印出来时将其更改回其旧值,但 ScrollPane 本身不会去任何地方并停留在顶部。它与删除和添加节点到 VBox 有关系吗?为什么设置 Vvalue 时 ScrollPane 不动?
【问题讨论】:
标签: java javafx scrollpane