Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFx:如何在listView中显示和隐藏滚动条?_Java_Javafx - Fatal编程技术网

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

JavaFx:如何在listView中显示和隐藏滚动条?,java,javafx,Java,Javafx,当鼠标指针不在listview上时,如何隐藏滚动条?当我的鼠标指针在listview上时显示滚动条。仅使用listview这是非常困难的,我甚至不确定这是否可能,因为listview仅根据需要显示滚动条,并且似乎无法访问滚动条(请参阅) 但使用ScrollPane有一个解决方法: 1.将ListView放在滚动窗格中 2.调整ListView的大小,使其始终足够大以显示连接内容(如果没有足够的空间,将在滚动窗格中展开) 3.仅当鼠标悬停滚动窗格时,使其滚动条变为apear 我测试的一个例子是:

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

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

但使用ScrollPane有一个解决方法: 1.将ListView放在滚动窗格中 2.调整ListView的大小,使其始终足够大以显示连接内容(如果没有足够的空间,将在滚动窗格中展开) 3.仅当鼠标悬停滚动窗格时,使其滚动条变为apear

我测试的一个例子是:

启动测试应用程序的应用程序类:

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);
        });
    }
}
包列表视图滚动条;
导入java.net.URL;
导入java.util.ResourceBundle;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.ListView;
导入javafx.scene.control.ScrollPane;
导入javafx.scene.control.ScrollPane.ScrollBarPolicy;
公共类ListViewScrollbarController实现可初始化{
@FXML
私有列表视图列表视图;
@FXML
私有滚动窗格;
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
//只需为示例添加一些文本
ObservableList String=FXCollections.observableArrayList(“字符串1”,
“非常长的字符串”、“字符串3”、“另一个字符串”);
setItems(字符串);
//在这里,您可以根据鼠标悬停在列表视图上来启用和禁用滚动条
scrollPane.SetonMouseCentered(e->{
scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
});
scrollPane.setOnMouseExited(e->{
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.NEVER);
});
}
}
现在,只要ListView的大小足够大,可以显示此解决方案应该执行的内容