gpt4 book ai didi

JavaFx:如何在listView中显示和隐藏滚动条?

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:34 29 4
gpt4 key购买 nike

当我的鼠标指针不在 ListView 上时如何隐藏滚动条?当我的鼠标指针位于 ListView 上时显示滚动条。

最佳答案

仅使用 ListView 这非常困难,我不确定这是否可能,因为 ListView 根据需要显示滚动条,并且似乎无法访问滚动条(请参阅 this post )。

但是有一个使用 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>

示例 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);
});
}
}

现在,只要 ListView 的大小足以显示此解决方法应该执行的内容。

关于JavaFx:如何在listView中显示和隐藏滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56438507/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com