我知道还有其他关于此的帖子,但这不是重复的。
如果我使用以下代码从TableView中隐藏水平滚动条,则表格底部仍然有1或2个像素区域,用户可以通过该区域水平滚动TableView ---用鼠标滚轮( SHIFT +鼠标滚轮也可以在桌面区域的任何地方使用。
.table-view *.scroll-bar:horizontal *.increment-button,
.table-view *.scroll-bar:horizontal *.decrement-button {
-fx-background-color: null;
-fx-background-radius: 0;
-fx-background-insets: 0;
-fx-padding: 0;
}
.table-view *.scroll-bar:horizontal *.increment-arrow,
.table-view *.scroll-bar:horizontal *.decrement-arrow {
-fx-background-color: null;
-fx-background-radius: 0;
-fx-background-insets: 0;
-fx-padding: 0;
-fx-shape: null;
}
我尝试了一些反射技巧来访问滚动条,并尝试以这种方式禁用它,但没有成功。有人有主意吗?
编辑-这是一个最小的示例,对我来说,它始终可以在Win 10 JavaFX 12.0.1上重现。
Test.java:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* Horizontal scrolling via mouse wheel is possible when you place the cursor at the bottom of the window, just above the resize-area.
*/
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
TableView<String> table = new TableView<>();
table.getColumns().add(new TableColumn<>("first"));
table.getColumns().add(new TableColumn<>("second"));
table.getColumns().add(new TableColumn<>("third"));
table.getColumns().add(new TableColumn<>("fourth"));
table.getColumns().add(new TableColumn<>("fifth"));
table.getColumns().add(new TableColumn<>("sixth"));
table.getColumns().add(new TableColumn<>("seventh"));
table.getColumns().add(new TableColumn<>("eighth"));
table.getColumns().add(new TableColumn<>("ninth"));
table.getColumns().add(new TableColumn<>("tenth"));
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
// table.addEventFilter(ScrollEvent.ANY, Event::consume);
ObservableList<String> data = FXCollections.observableArrayList();
for (int i = 0; i < 5000; i++) {
data.add("foobar");
}
table.setItems(data);
StackPane root = new StackPane();
root.getChildren().add(table);
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add("test.css");
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(Test.class);
}
}
Test.CSS:
.table-view .scroll-bar * {
-fx-min-width: 0;
-fx-pref-width: 0;
-fx-max-width: 0;
-fx-min-height: 0;
-fx-pref-height: 0;
-fx-max-height: 0;
}
答案 0 :(得分:1)
如果您不想使用CSS解决方案,则可以这样做
public static <T extends Control> void removeScrollBar(T table) {
ScrollBar scrollBar = (ScrollBar) table.queryAccessibleAttribute(AccessibleAttribute.HORIZONTAL_SCROLLBAR);
/*
*This null-check is for safety reasons if you are using when the table's skin isn't yet initialized.
* If you use this method in a custom skin you wrote, where you @Override the layoutChildren method,
* use it there, and it should be always initialized, so null-check would be unnecessary.
*
*/
if (scrollBar != null) {
scrollBar.setPrefHeight(0);
scrollBar.setMaxHeight(0);
scrollBar.setOpacity(1);
scrollBar.setVisible(false); // If you want to keep the scrolling functionality then delete this row.
}
}