将特定列设置为启动程序时第一个看到的列
Setting specific column to be first one to see when starting a program
我正在做一个项目,我有一个 tableView,它包含许多列(实际上是 365 个)。我想知道是否可以将特定列设置在中间,例如,如果我有 365 天,我希望用户无需左右滚动即可看到当前日期的列。
如果允许特定列移动到左边缘,下面的代码会更好。 (你可能已经知道了。)
tableView.scrollToColumn(anyColumn);
要滚动到列居中的位置,以下方法可能有效。
public class TableViewUtil {
public static void centerColumn(TableView<?> tableView, TableColumn<?, ?> column) {
findScrollBar(tableView, Orientation.HORIZONTAL).ifPresent(scroll -> {
final double offset = getLeftOffset(tableView, column);
final double target = offset - tableView.getWidth() / 2.0 + column.getWidth() / 2.0;
scroll.setValue(Math.min(Math.max(target, scroll.getMin()), scroll.getMax()));
});
}
private static double getLeftOffset(TableView<?> tableView, TableColumn<?, ?> column) {
double offset = 0.0;
for (TableColumn<?,?> c: tableView.getColumns()) {
if (c == column) return offset;
if (c.isVisible()) offset += c.getWidth();
}
return offset;
}
private static Optional<ScrollBar> findScrollBar(TableView<?> tableView, Orientation orientation) {
return tableView.lookupAll(".scroll-bar").stream()
.filter(node -> node instanceof ScrollBar && ((ScrollBar)node).getOrientation() == orientation)
.map(node -> ((ScrollBar)node))
.findFirst();
}
}
例如 initialize()
,
Platform.runLater(() -> TableViewUtil.centerColumn(tableView, anyColumn));
我正在做一个项目,我有一个 tableView,它包含许多列(实际上是 365 个)。我想知道是否可以将特定列设置在中间,例如,如果我有 365 天,我希望用户无需左右滚动即可看到当前日期的列。
如果允许特定列移动到左边缘,下面的代码会更好。 (你可能已经知道了。)
tableView.scrollToColumn(anyColumn);
要滚动到列居中的位置,以下方法可能有效。
public class TableViewUtil {
public static void centerColumn(TableView<?> tableView, TableColumn<?, ?> column) {
findScrollBar(tableView, Orientation.HORIZONTAL).ifPresent(scroll -> {
final double offset = getLeftOffset(tableView, column);
final double target = offset - tableView.getWidth() / 2.0 + column.getWidth() / 2.0;
scroll.setValue(Math.min(Math.max(target, scroll.getMin()), scroll.getMax()));
});
}
private static double getLeftOffset(TableView<?> tableView, TableColumn<?, ?> column) {
double offset = 0.0;
for (TableColumn<?,?> c: tableView.getColumns()) {
if (c == column) return offset;
if (c.isVisible()) offset += c.getWidth();
}
return offset;
}
private static Optional<ScrollBar> findScrollBar(TableView<?> tableView, Orientation orientation) {
return tableView.lookupAll(".scroll-bar").stream()
.filter(node -> node instanceof ScrollBar && ((ScrollBar)node).getOrientation() == orientation)
.map(node -> ((ScrollBar)node))
.findFirst();
}
}
例如 initialize()
,
Platform.runLater(() -> TableViewUtil.centerColumn(tableView, anyColumn));