java JavaFX 自动向下滚动滚动窗格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13156896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX auto-scroll down scrollpane
提问by nailujed
Is there any way of auto scroll down a ScrollPane control when it content's height increases ? For example, I have a TitledPane on the bottom of the screen (inside a ScrollPane) and when I expand it I would like the ScrollPane to scroll down so I can see the entire content of the TitledPane.
当内容的高度增加时,是否有任何方法可以自动向下滚动 ScrollPane 控件?例如,我在屏幕底部(在 ScrollPane 内)有一个 TitledPane,当我展开它时,我希望 ScrollPane 向下滚动,以便我可以看到 TitledPane 的全部内容。
回答by Jeankowkow
You can bind
the ScrollPane
vvalue
property with the heightProperty
of the inner container. For example, if you have a VBox
in your ScrollPane
:
您可以使用内部容器bind
的ScrollPane
vvalue
属性heightProperty
。例如,如果您VBox
的ScrollPane
:
scrollPane.vvalueProperty().bind(vBox.heightProperty());
回答by amru
You can achieve that behaviour With combination of titledPane.localToScene()
and scrollPane.setVvalue()
您可以通过titledPane.localToScene()
和scrollPane.setVvalue()
The first is to get titledPane's coordinates while the second is to set scrollPane's vertical bar position. Note that it's range is between 0 - 1.
第一个是获取 titledPane 的坐标,第二个是设置 scrollPane 的垂直条位置。请注意,它的范围在 0 - 1 之间。
回答by Guerino Rodella
You have to tell scrollpane where is the current coordinates before pass a value to the vertical or horizontal bar.
在将值传递给垂直或水平条之前,您必须告诉滚动窗格当前坐标在哪里。
This code works pretty fine for me:
这段代码对我来说很好用:
// the owner's node of your scrollPane;
titledPane.layout();
// the maxValue for scrollPane bar ( 1.0 it's the default value )
scrollPane.setVvalue( 1.0d );
You have to code this where your scrollpane grows. For example, if you add nodes to the scrollpane inner node, them, add a listener to it's children list.
您必须在滚动窗格增长的地方对此进行编码。例如,如果您将节点添加到滚动窗格内部节点,则将它们添加到其子列表中的侦听器。
If the inner node of scrollpane change it's height, add a listener to the heightProperty.
如果滚动窗格的内部节点更改其高度,请向 heightProperty 添加一个侦听器。
For example, the inner node of your scrollPane it's an AnchorPane and you add nodes to this pane, so do like this:
例如, scrollPane 的内部节点是一个 AnchorPane 并且您将节点添加到此窗格中,因此请执行以下操作:
anchorPane.getChildren().addListener(
( ListChangeListener.Change<? extends Node> c ) -> {
titledPane.layout();
scrollPane.setVvalue( 1.0d );
}
);
If it's the height which grows...
如果是身高增长...
heightProperty().addListener(
(observable, oldValue, newValue) -> {
titledPane.layout();
scrollPane.setVvalue( 1.0d );
}
);
That's it!
而已!
回答by Tamir Croll
You can add a listener to the TitledPane's height property like that:
您可以像这样向 TitledPane 的高度属性添加一个侦听器:
titledPane.heightProperty().addListener((observable, oldValue, newValue) ->
vvalueProperty().set(newValue.doubleValue()));
回答by el-aasi
Donno if it is still relevant, I have had a similar issue-ish and this solution worked for me. I needed the scrollbar to auto scroll in an MVVM pattern. What I did was to bind the textarea's scrollTopProperty in the View with a SimpleDoubleProperty in the ViewModel. The only issue is that the scrollTopProperty, basically scrolls based on pixel so I increased it based on the number of lanes * 100;
Donno,如果它仍然相关,我有一个类似的问题,这个解决方案对我有用。我需要滚动条以 MVVM 模式自动滚动。我所做的是将视图中 textarea 的 scrollTopProperty 与 ViewModel 中的 SimpleDoubleProperty 绑定。唯一的问题是 scrollTopProperty 基本上是基于像素滚动的,所以我根据车道数 * 100 增加了它;
View
看法
textArea.textProperty().bindBidirectional(viewModel.SimpleStringProperty());
textArea.scrollTopProperty().bindBidirectional(viewModel.SimpleDoubleProperty());
ViewModel
视图模型
SimpleDoubleProperty.setValue(SimpleStringProperty.getValue().split("\n").length * 100);
I know that the code is not really representative, but if someone needs more details of the implementations I can elaborate...
我知道代码并不是真正具有代表性的,但是如果有人需要更多的实现细节,我可以详细说明......
回答by henrik dryselius
I did it by using an AnimationTimer. I had to wait for 100000000 nanoseconds to be sure that the ScrollPane had reacted to the expanded Titlepane.
我是通过使用 AnimationTimer 做到的。我不得不等待 100000000 纳秒以确保 ScrollPane 对扩展的 Titlepane 做出反应。
public void scrollNodeInTopScrollPane(Node n, ScrollPane s) {
final Node node = n;
final ScrollPane clientTopScrollPane = s;
AnimationTimer timer = new AnimationTimer() {
long lng = 0;
@Override
public void handle(long l) {
if (lng == 0) {
lng = l;
}
if (l > lng + 100000000) {
if (node.getLocalToSceneTransform().getTy() > 20) {
clientTopScrollPane.setVvalue(clientTopScrollPane.getVvalue() + 0.05);
if (clientTopScrollPane.getVvalue() == 1) {
this.stop();
}
} else {
this.stop();
}
}
}
};
timer.start();
}