我想隐藏或删除 TableView 中的垂直滚动条。我想用我自己的 PgUp/PgDown 按钮管理滚动。
按钮在下面的代码中工作正常,但 verticalScrollBar 破坏了所需的方面。如何隐藏/删除它?
btnPgDown.setOnAction(e -> {
if (table.getSelectionModel().getSelectedIndex() == -1) {
table.getSelectionModel().select(0);
}
Event.fireEvent(table, EventUtil.PG_DN_PR_EVENT);
table.requestFocus();
});
Run Code Online (Sandbox Code Playgroud)
注意:EventUtil.PG_DN_PR_EVENT 模拟 PgDown 键事件。
我认为隐藏 TableView 滚动条的最佳方法是使用 CSS 代码。
这是垂直滚动条的代码。
.table-view *.scroll-bar:vertical *.increment-button,
.table-view *.scroll-bar:vertical *.decrement-button {
-fx-background-color: null;
-fx-background-radius: 0;
-fx-background-insets: 0;
-fx-padding: 0;
}
.table-view *.scroll-bar:vertical *.increment-arrow,
.table-view *.scroll-bar:vertical *.decrement-arrow {
-fx-background-color: null;
-fx-background-radius: 0;
-fx-background-insets: 0;
-fx-padding: 0;
-fx-shape: null;
}
Run Code Online (Sandbox Code Playgroud)
如果您还想隐藏水平滚动条,则必须添加相同的代码,但更改*.scroll-bar:vertical为*.scroll-bar:horizontal.
如果您想通过代码滚动表格,我建议使用包含在 TableView 类中的scrollTo方法。
根据我自己的经验, a 是否TableView显示 aScrollBar取决于 上的按钮和箭头ScrollBar。
在我的项目中,我使用这个 CSS 代码来隐藏两个滚动条
.table-view *.increment-button,
.table-view *.decrement-button,
.table-view *.increment-arrow,
.table-view *.decrement-arrow
{
-fx-padding: 0;
-fx-shape: "";
}
Run Code Online (Sandbox Code Playgroud)
但这有副作用,因为显然TableMenuButton使用相同的填充,TableMenuButton填充为 0 时消失,我唯一看到的是通常显示在按钮上的“+”号的一半。所以这是一种方式,但不一定是方式。