我正在尝试获取有关ScrollBar
标准包含在ScrollPane
. 特别是我有兴趣阅读height
水平的Scrollbar
。我如何参考它?
问问题
13307 次
3 回答
8
我认为您可以使用 Node 类的 lookupAll() 方法来查找滚动条。 http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#lookupAll(java.lang.String)
例如:
package com.test;
import java.util.Set;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPaneBuilder;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
public class JavaFxScrollPaneTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
String longString = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.";
Text longText = TextBuilder.create().text(longString).build();
ScrollPane scrollPane = ScrollPaneBuilder.create().content(longText).build();
primaryStage.setScene(new Scene(scrollPane, 400, 100));
primaryStage.show();
Set<Node> nodes = scrollPane.lookupAll(".scroll-bar");
for (final Node node : nodes) {
if (node instanceof ScrollBar) {
ScrollBar sb = (ScrollBar) node;
if (sb.getOrientation() == Orientation.HORIZONTAL) {
System.out.println("horizontal scrollbar visible = " + sb.isVisible());
System.out.println("width = " + sb.getWidth());
System.out.println("height = " + sb.getHeight());
}
}
}
}
}
于 2012-11-27T04:07:05.917 回答
5
这不是最好的做法,但有效,
private boolean determineVerticalSBVisible(final ScrollPane scrollPane) {
try {
final ScrollPaneSkin skin = (ScrollPaneSkin) scrollPane.getSkin();
final Field field = skin.getClass().getDeclaredField("vsb");
field.setAccessible(true);
final ScrollBar scrollBar = (ScrollBar) field.get(skin);
field.setAccessible(false);
return scrollBar.isVisible();
} catch (final Exception e) {
e.printStackTrace();
}
return false;
}
水平滚动条使用“hsb”。
最好的问候,恩里克·盖德斯。
于 2013-01-23T11:03:02.753 回答
5
由于上述方法不适用于所有人(包括我),因此我对其进行了更多调查并找到了问题的根源。
一般来说,这两种方法都有效,但只有在设置了ScrollPane
'skin
属性后才有效。就我而言,skin
在null
使用FXMLLoader
.
skin
通过在属性尚未初始化的情况下延迟调用(使用一次性侦听器)可以解决问题。
工作样板代码:
ScrollPane scrollPane;
// ...
if (scrollPane.getSkin() == null) {
// Skin is not yet attached, wait until skin is attached to access the scroll bars
ChangeListener<Skin<?>> skinChangeListener = new ChangeListener<Skin<?>>() {
@Override
public void changed(ObservableValue<? extends Skin<?>> observable, Skin<?> oldValue, Skin<?> newValue) {
scrollPane.skinProperty().removeListener(this);
accessScrollBar(scrollPane);
}
};
scrollPane.skinProperty().addListener(skinChangeListener);
} else {
// Skin is already attached, just access the scroll bars
accessScrollBar(scrollPane);
}
private void accessScrollBar(ScrollPane scrollPane) {
for (Node node : scrollPane.lookupAll(".scroll-bar")) {
if (node instanceof ScrollBar) {
ScrollBar scrollBar = (ScrollBar) node;
if (scrollBar.getOrientation() == Orientation.HORIZONTAL) {
// Do something with the horizontal scroll bar
// Example 1: Print scrollbar height
// System.out.println(scrollBar.heightProperty().get());
// Example 2: Listen to visibility changes
// scrollBar.visibleProperty().addListener((observable, oldValue, newValue) -> {
// if(newValue) {
// // Do something when scrollbar gets visible
// } else {
// // Do something when scrollbar gets hidden
// }
// });
}
if (scrollBar.getOrientation() == Orientation.VERTICAL) {
// Do something with the vertical scroll bar
}
}
}
}
于 2019-04-30T11:03:15.410 回答