当鼠标指针不在列表视图中时如何隐藏滚动条?并在我的鼠标指针位于列表视图时显示滚动条。
答案 0 :(得分:0)
仅使用ListView很难,而且我不确定是否可行,因为ListView会根据需要显示滚动条,并且似乎无法访问滚动条(请参见this post)。 / p>
但是有一种使用ScrollPane的解决方法: 1.将ListView放入ScrollPane 2.调整ListView的大小,使其始终足够大以显示内容(如果没有足够的空间,它将在ScrollPane中展开) 3.仅在用鼠标悬停时才使ScrollPane的滚动条出现
我测试的示例:
用于启动测试应用程序的Application类:
package listViewScrollbar;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ListViewScrollbarApplication extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
URL fxmlUrl = getClass().getResource("ListViewScrollbarTest.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(fxmlUrl);
ListViewScrollbarController controller = new ListViewScrollbarController();
fxmlLoader.setController(controller);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root, 200, 300);
scene.getStylesheets().add(getClass().getResource("ListViewScrollbar.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
fxml文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<AnchorPane xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<BorderPane layoutX="-89.0" layoutY="-123.0" prefHeight="300.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<ScrollPane fx:id="scrollPane" BorderPane.alignment="CENTER">
<content>
<ListView fx:id="listView" prefHeight="500.0" prefWidth="500.0" />
</content>
</ScrollPane>
</center>
</BorderPane>
</children>
</AnchorPane>
示例控制器:
package listViewScrollbar;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
public class ListViewScrollbarController implements Initializable {
@FXML
private ListView<String> listView;
@FXML
private ScrollPane scrollPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
//just add some text for the example
ObservableList<String> strings = FXCollections.observableArrayList("String 1",
"A very long String ...............................................................", "String 3", "Another String");
listView.setItems(strings);
//here you enable and disable the scroll bars depending on the mouse hovering the list view
scrollPane.setOnMouseEntered(e -> {
scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
});
scrollPane.setOnMouseExited(e -> {
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.NEVER);
});
}
}
现在,只要ListView的大小足以显示此替代方法应执行的内容。