gpt4 book ai didi

JavaFX TreeView 恢复滚动状态

转载 作者:行者123 更新时间:2023-11-29 04:43:46 25 4
gpt4 key购买 nike

我想保存 TreeView 组件的当前滚动状态并随时恢复该状态。

我尝试了很多方法来获取滚动位置并将其设置回去,但无论如何都做不到。

这里的关键点是我们需要保存min,max,value,unitInc,blockInc的值,并在需要的时候恢复这些值。只有 value 或只有 min 和 max 不足以正确恢复。

更新

监听垂直值变化并存储滚动状态

Set<Node> nodes = this.treeView.lookupAll(".scroll-bar");
for (Node node : nodes) {
ScrollBar scrollBar = getScrollBar(node);
if (scrollBar.getOrientation() == Orientation.VERTICAL) {
scrollBar.valueProperty().addListener((observable, oldValue, newValue) -> {
double value = newValue.doubleValue();
if (value > 0) {
scrollState.setMin(scrollBar.getMin());
scrollState.setMax(scrollBar.getMax());
scrollState.setUnitIncrement(scrollBar.getUnitIncrement());
scrollState.setBlockIncrement(scrollBar.getBlockIncrement());
scrollState.setVerticalValue(value);
}
});
}
}

在这里也存储滚动状态

private ScrollBar getScrollBar(Node node) {
ScrollBar scrollBar = (ScrollBar) node;
if (Objects.nonNull(scrollBar)) {
scrollState.setMin(scrollBar.getMin());
scrollState.setMax(scrollBar.getMax());
scrollState.setUnitIncrement(scrollBar.getUnitIncrement());
scrollState.setBlockIncrement(scrollBar.getBlockIncrement());
}

return scrollBar;
}

恢复存储的滚动状态

threadService.schedule(() -> { // run after some ms, it is important
threadService.runActionLater(() -> { // run in ui thread

Set<Node> nodes = this.treeView.lookupAll(".scroll-bar");
for (Node node : nodes) {
ScrollBar scrollBar = getScrollBar(node);
if (scrollBar.getOrientation() == Orientation.VERTICAL) {
double verticalValue = scrollState.getVerticalValue();
if (verticalValue > 0) {
scrollBar.setMin(scrollState.getMin());
scrollBar.setMax(scrollState.getMax());
scrollBar.setUnitIncrement(scrollState.getUnitIncrement());
scrollBar.setBlockIncrement(scrollState.getBlockIncrement());
scrollBar.setValue(verticalValue);
}
}
}
}, true);
}, 50, TimeUnit.MILLISECONDS);

最佳答案

如果您只想滚动到 TreeView 中的索引,那么您可以使用 scrollTo方法(例如,您保存选定的索引,然后稍后滚动到该索引):

tree.scrollTo(5);

如果您确实想要存储然后恢复 ScrollBar 的位置,您可以使用lookupAll Node 类的方法来查找 .scroll-bar 样式,然后将其大小写为 ScrollBar 对象,该对象具有 minProperty , maxPropertyvalueProperty存储。

更新:

根据 OP 的发现,unitIncrementPropertyblockIncrementProperty ScrollBar 也应该被存储和恢复以正确支持操作。

示例

在示例中,我用一些虚拟数据填充了 TreeView。它包含一个Button,用于将水平和垂直ScrollBar的位置保存到一个成员对象中,该成员对象可以存储最小值,最大值,值,blockIncrement和unitIncrement值位置。它还有一个 Button 来恢复上次保存的滚动条位置。

public class TreeViewSample extends Application {

// Members to store the horizontal and vertical positions
private ScrollBarState hScrollBarState = null;
private ScrollBarState vScrollBarState = null;

TreeView<String> tree;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Tree View Sample with scrollbars");

TreeItem<String> rootItem = new TreeItem<String>("Items");
rootItem.setExpanded(true);
for (int i = 1; i < 30; i++) {
TreeItem<String> item = new TreeItem<String>("Long long long long long long long Item" + i);
rootItem.getChildren().add(item);
}

tree = new TreeView<String>(rootItem);
VBox root = new VBox();


Button buttonSave = new Button("Save ScrollBars!");
buttonSave.setOnAction((event) -> {
// On save get the scrollbars and update the members (if the scrollbar is present)
ScrollBar bar = getScrollBar(tree, Orientation.HORIZONTAL);
if (bar != null)
hScrollBarState = new ScrollBarState(bar.getMin(), bar.getMax(), bar.getValue(), bar.getBlockIncrement(), bar.getUnitIncrement());

bar = getScrollBar(tree, Orientation.VERTICAL);

if (bar != null)
vScrollBarState = new ScrollBarState(bar.getMin(), bar.getMax(), bar.getValue(), bar.getBlockIncrement(), bar.getUnitIncrement());

});

Button buttonRestore = new Button("Restore ScrollBars!");
buttonRestore.setOnAction((event) -> {

restoreScrollBarPositions(getScrollBar(tree, Orientation.HORIZONTAL), hScrollBarState);
restoreScrollBarPositions(getScrollBar(tree, Orientation.VERTICAL), vScrollBarState);
});

root.getChildren().addAll(tree, buttonSave, buttonRestore);

primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}

private ScrollBar getScrollBar(TreeView<?> tree, Orientation orientation) {
// Get the ScrollBar with the given Orientation using lookupAll
for (Node n : tree.lookupAll(".scroll-bar")) {
if (n instanceof ScrollBar) {
ScrollBar bar = (ScrollBar) n;

if (bar.getOrientation().equals(orientation))
return bar;
}
}
return null;
}

private void restoreScrollBarPositions(ScrollBar bar, ScrollBarState state) {
// Set back the position values if they present
if (bar != null && state != null) {
bar.setMin(state.min);
bar.setMax(state.max);
bar.setValue(state.value);
bar.setUnitIncrement(state.unitIncrement);
bar.setBlockIncrement(state.blockIncrement);
}
}

// Simple class to store the position values
class ScrollBarState {
double min;
double max;
double value;
double blockIncrement;
double unitIncrement;

ScrollBarState(double min, double max, double value, double blockInc, double unitInc) {
this.min = min;
this.max = max;
this.value = value;
this.blockIncrement = blockInc;
this.unitIncrement = unitInc;
}
}
}

关于JavaFX TreeView 恢复滚动状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38101041/

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