我对编码很新。我通过硬编码填充值来创建演示表视图。我希望将ms excel滚动拖动功能应用于我的表视图。 我无法为此获得解决方案。 代码片段会很有帮助。
我的演示表
我尝试实现以下代码:
private void setupScrolling() {
scrolltimeline.setCycleCount(Timeline.INDEFINITE);
scrolltimeline.getKeyFrames().add(new KeyFrame(Duration.millis(20), "Scoll", (ActionEvent) -> { dragScroll();}));
table.setOnDragExited(event -> {
if (event.getX() > 0) {
scrollDirection = 1.0 / table.getSelectionModel().getSelectedIndex();
}
else {
scrollDirection = -1.0 / table.getSelectionModel().getSelectedIndex();
}
scrolltimeline.play();
});
table.setOnDragEntered(event -> {
scrolltimeline.stop();
});
table.setOnDragDone(event -> {
scrolltimeline.stop();
});
}
private void dragScroll() {
ScrollBar sb = getHorizontalScrollBar();
if (sb != null) {
double newValue = sb.getValue() + scrollDirection;
newValue = Math.min(newValue, 1.0);
newValue = Math.max(newValue, 0.0);
sb.setValue(newValue);
}
}
private ScrollBar getHorizontalScrollBar() {
ScrollBar result = null;
for (Node n : table.lookupAll(".scroll-bar")) {
if (n instanceof ScrollBar) {
ScrollBar bar = (ScrollBar) n;
if (bar.getOrientation().equals(Orientation.HORIZONTAL)) {
result = bar;
}
}
}
return result;
}
VBox vbox = new VBox();
vbox.getChildren().addAll(table);
scrollPane.setContent(vbox);
scrollPane.setPannable(true);
Scene scene = new Scene(scrollPane);