ListView滚动条行为

ListView滚动条行为,listview,javafx,javafx-8,Listview,Javafx,Javafx 8,通常,将滚动条移动到可滚动窗口的中间时,文本位置将是静态的,即使在底部添加更多文本时也是如此;相反,滚动条的光标会慢慢向上移动。 ListVIEW显示了相反的行为:当滚动条的光标位于中间的某个位置时,它保持其位置,而文本向上移动。参见示例程序: import javafx.application.Application; import javafx.application.Platform; import javafx.collections.FXCollections; import java

通常,将滚动条移动到可滚动窗口的中间时,文本位置将是静态的,即使在底部添加更多文本时也是如此;相反,滚动条的光标会慢慢向上移动。
ListVIEW显示了相反的行为:当滚动条的光标位于中间的某个位置时,它保持其位置,而文本向上移动。参见示例程序:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ListViewTest extends Application  {

    ListView<String> listview= new ListView<String>();
    private ObservableList<String> items =FXCollections.observableArrayList ();

    public void start(Stage stage) {
        listview.setItems(items);
        Scene scene = new Scene(new StackPane());
        final VBox vbox = new VBox();
        vbox.getChildren().addAll(listview);
        ((StackPane) scene.getRoot()).getChildren().add(vbox);
        stage.setScene(scene);
        stage.setHeight(200);
        stage.show();

        Task<Integer> testTask = new Task<Integer>() {
            @Override protected Integer call() throws Exception {
                try {Thread.sleep(500);} catch (InterruptedException e) {   }
                for (int i=0;i<50;i++) {
                    try {Thread.sleep(300);} catch (InterruptedException e) {   }
                    final int j=i;
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            items.add(""+j);
                        }
                    });
                }
                return 0;
            }
        };
        new Thread(testTask).start();
    }
}
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.concurrent.Task;
导入javafx.scene.scene;
导入javafx.scene.control.ListView;
导入javafx.scene.layout.StackPane;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类ListViewTest扩展了应用程序{
ListView ListView=新建ListView();
私有ObservableList items=FXCollections.observableArrayList();
公众假期开始(阶段){
setItems(项目);
场景=新场景(新StackPane());
最终VBox VBox=新的VBox();
vbox.getChildren().addAll(listview);
((StackPane)scene.getRoot()).getChildren().add(vbox);
舞台场景;
舞台设置高度(200);
stage.show();
任务testTask=新任务(){
@重写受保护的整数调用()引发异常{
试试{Thread.sleep(500);}catch(InterruptedException e){}

对于(int i=0;iI’将为用户提供一种暂停和恢复更新的方法,因此在浏览消息时ListView不会移动。(我还是希望s.o.能想出更好的解决方案)