问题描述
我的问题是我有两个水平滚动条,我希望它们齐声滚动,我尝试使用:
My problem is I have two horizontal scroll bars that I want them to scroll in unison and I've tried using:
bar1。 valueProperty()。bindBidirectional(bar2.valueProperty());
问题是我注意到 bar1
的最大值是 1.0
,而 bar2
的最大值是 102.5
。因此,问题是,当我滚动 bar2
时, bar1
由于其价值差异很大而移动很多。我尝试过使用 setMin
, setMax
, setUnitIncrement
,绑定value属性之前 setBlockIncrement
。但是不起作用。
The problem is that i notice bar1
's max value is 1.0
while bar2
's max value is 102.5
. Hence, the problem, when i scroll bar2
, bar1
moves a lot due to the large discrepancy in their value. I've tried using setMin
, setMax
, setUnitIncrement
, setBlockIncrement
before binding the value property. But is doesn't work.
public class DynamicTableView extends Application {
private Scene scene;
private VBox root;
private ScrollPane scPane;
private static final int N_COLS = 10;
private static final int N_ROWS = 10;
private Button button;
public void start(Stage stage) throws Exception {
root = new VBox();
scPane = new ScrollPane();
Label lbl = new Label("Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table Dynamic Table");
scPane.setContent(lbl);
root.getChildren().add(scPane);
TestDataGenerator dataGenerator = new TestDataGenerator();
TableView<ObservableList<String>> tableView = new TableView<>();
// add columns
List<String> columnNames = dataGenerator.getNext(N_COLS);
for (int i = 0; i < columnNames.size(); i++) {
final int finalIdx = i;
TableColumn<ObservableList<String>, String> column = new TableColumn<>(
columnNames.get(i)
);
column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx)));
tableView.getColumns().add(column);
}
// add data
for (int i = 0; i < N_ROWS; i++) {
tableView.getItems().add(
FXCollections.observableArrayList(
dataGenerator.getNext(N_COLS)
)
);
}
root.getChildren().add(tableView);
tableView.setPrefHeight(200);
button = new Button("Delete");
button.setOnAction(event -> { tableView.getItems().clear();});
root.getChildren().add(button);
Scene scene = new Scene(root, 600, 600);
stage.setScene(scene);
stage.show();
for(Node node1: scPane.lookupAll(".scroll-bar"))
{
if(node1 instanceof ScrollBar)
{
ScrollBar scrollBar1 = (ScrollBar)node1;
if(scrollBar1.getOrientation() == Orientation.HORIZONTAL)
{
for(Node node2: tableView.lookupAll(".scroll-bar"))
{
if(node2 instanceof ScrollBar)
{
ScrollBar scrollBar2 = (ScrollBar)node2;
if(scrollBar2.getOrientation() == Orientation.HORIZONTAL)
{
scrollBar2.valueProperty().bindBidirectional(scrollBar1.valueProperty());
break;
}
}
}
}
}
}
}
public static void main(String[] args) {
launch(args);
}
private static class TestDataGenerator {
private static final String[] LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tempus cursus diam ac blandit. Ut ultrices lacus et mattis laoreet. Morbi vehicula tincidunt eros lobortis varius. Nam quis tortor commodo, vehicula ante vitae, sagittis enim. Vivamus mollis placerat leo non pellentesque. Nam blandit, odio quis facilisis posuere, mauris elit tincidunt ante, ut eleifend augue neque dictum diam. Curabitur sed lacus eget dolor laoreet cursus ut cursus elit. Phasellus quis interdum lorem, eget efficitur enim. Curabitur commodo, est ut scelerisque aliquet, urna velit tincidunt massa, tristique varius mi neque et velit. In condimentum quis nisi et ultricies. Nunc posuere felis a velit dictum suscipit ac non nisl. Pellentesque eleifend, purus vel consequat facilisis, sapien lacus rutrum eros, quis finibus lacus magna eget est. Nullam eros nisl, sodales et luctus at, lobortis at sem.".split(" ");
private int curWord = 0;
List<String> getNext(int nWords) {
List<String> words = new ArrayList<>();
for (int i = 0; i < nWords; i++) {
if (curWord == Integer.MAX_VALUE) {
curWord = 0;
}
words.add(LOREM[curWord % LOREM.length]);
curWord++;
}
return words;
}
}
}
推荐答案
Bidi-将scrollPane中scrollBar的valueProperty绑定到tableView中scrollBar的值,因为前者是规范化的,而后者不是。相反,请手动监听更改和缩放,例如:
Bidi-Binding of valueProperty of a scrollBar in a scrollPane to that of a scrollBar in a tableView is not possible because the former is normalized while the latter is not. Instead, listen to changes and scale manually, like:
protected void bindScrollBarValues(ScrollBar scrollBarInTable, ScrollBar scrollBarInPane) {
// can't use bidi-binding because bar in scrollPane is normalized, bar in table is not
// scrollBarInTable.valueProperty().bindBidirectional(scrollBarInPane.valueProperty());
// scale manually
scrollBarInTable.valueProperty().addListener((src, ov, nv) -> {
double tableMax = scrollBarInTable.getMax();
scrollBarInPane.setValue(nv.doubleValue() / tableMax);
});
scrollBarInPane.valueProperty().addListener((src, ov, nv) -> {
double tableMax = scrollBarInTable.getMax();
scrollBarInTable.setValue(nv.doubleValue() * tableMax);
});
}
这篇关于同步两个滚动条JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!