Java 将特定列设置为启动程序时要查看的第一列
我正在做一个项目,我有一个tableView,它由许多列组成(实际上是365列)。我想知道是否有可能在中间设置一个特定的列,例如,如果我有365天,我希望用户能够在不滚动左右的情况下看到当前日期的列。 < P>如果一个特定的列被允许移到左边,那么下面的代码就更好了。(你可能已经知道了。)Java 将特定列设置为启动程序时要查看的第一列,java,javafx,tableview,fxml,Java,Javafx,Tableview,Fxml,我正在做一个项目,我有一个tableView,它由许多列组成(实际上是365列)。我想知道是否有可能在中间设置一个特定的列,例如,如果我有365天,我希望用户能够在不滚动左右的情况下看到当前日期的列。 < P>如果一个特定的列被允许移到左边,那么下面的代码就更好了。(你可能已经知道了。) 要滚动到柱的中心位置,可以使用以下方法 public class TableViewUtil { public static void centerColumn(TableView<?>
要滚动到柱的中心位置,可以使用以下方法
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();
}
}
design
标签不适合您的问题。
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();
}
}
Platform.runLater(() -> TableViewUtil.centerColumn(tableView, anyColumn));