How to hide scrollbar when my mouse pointer is NOT on the listview? And show scrollbar when my mouse pointer is on the listview.
Asked
Active
Viewed 2,945 times
1 Answers
3
Using only a ListView this is very hard and I'm not sure if it's even possible because the ListView shows the scrollbars just as needed and there seems to be no access to the scrollbars (see this post).
But there is a workaround using ScrollPane: 1. Put the ListView in a ScrollPane 2. Adapt the size of the ListView so that it will always be big enough to show the conntent (if there is not enough space it will be expanded within the ScrollPane) 3. Make the ScrollPane's scrollbars apear only when hovering it with the mouse
An example I tested:
The Application class to start the test 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();
}
}
}
The fxml file:
<?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>
The example controller:
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);
});
}
}
Now as long as the ListView's size is big enough to show the content this workaround should do.

Tobias
- 2,547
- 3
- 14
- 29