JavaFXTreeView向下滚动后跳转到顶部

t0ybt7op  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(301)

每当我向下滚动一个树视图时,由于某种原因,它会跳回到开头。
这是我的fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pl.gieted.timetable.client.javafx.controllers.GroupSelectionController">
   <Text layoutX="46.0" layoutY="77.0" stroke="BLACK" strokeType="OUTSIDE" strokeWidth="0.0" text="Wybierz grupę:" wrappingWidth="395.849609375" AnchorPane.leftAnchor="46.0" AnchorPane.topAnchor="39.2314453125">
      <font>
         <Font size="35.0" />
      </font>
   </Text>
   <TreeView fx:id="timetableSelectionTree" layoutX="64.0" layoutY="138.0" prefHeight="537.0" prefWidth="316.0" showRoot="false" AnchorPane.bottomAnchor="45.0" AnchorPane.leftAnchor="64.0" AnchorPane.topAnchor="138.0" />
   <Button layoutX="1165.0" layoutY="650.0" mnemonicParsing="false" onAction="#onNextButtonClicked" prefHeight="24.0" prefWidth="66.0" text="Dalej" AnchorPane.bottomAnchor="45.0" AnchorPane.rightAnchor="49.0" />
</AnchorPane>

我的控制器:

package pl.gieted.timetable.client.javafx.controllers;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import pl.gieted.timetable.client.timetable.TimetableService;
import pl.gieted.timetable.client.timetable.options.TimetableOption;

import javax.inject.Inject;
import javax.swing.event.ChangeEvent;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

public class GroupSelectionController implements Initializable {
    private final TimetableService timetableService;

    @FXML
    private TreeView<TimetableOption> timetableSelectionTree;

    @Inject
    public GroupSelectionController(TimetableService timetableService) {
        this.timetableService = timetableService;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        timetableSelectionTree.setCellFactory(tree -> new TreeCell<>() {

            @Override
            protected void updateItem(TimetableOption item, boolean empty) {
                if (item != null) {
                    setText(item.getName());
                }
            }
        });

        try {
            List<TreeItem<TimetableOption>> options = timetableService.getRootList().stream().map(TreeItem::new).collect(Collectors.toList());
            TreeItem<TimetableOption> root = new TreeItem<>();
            root.getChildren().addAll(options);
            root.setExpanded(true);
            timetableSelectionTree.setRoot(root);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void onNextButtonClicked() {

    }
}

有人能告诉我为什么会这样,怎么让它停止吗?
这是这个行为的录像
这是一个非常基本的用法,所以我不知道为什么会发生这种情况

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题