首页 > 解决方案 > 动态添加行时 TreeTableView 内容消失

问题描述

我有这个小示例代码:

主类:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

public static void main(String[] args) {
    Application.launch(args);
}

@Override
public void start(Stage stage) throws IOException {
    stage.setTitle("Tree Table View Samples");
    FXMLLoader loader = new FXMLLoader();
    Parent sceneRoot = loader.load(getClass().getResource("sample.fxml"));
    Controller controller = new Controller();
    loader.setController(controller);
    final Scene scene = new Scene(sceneRoot, 450, 400);
    stage.setScene(scene);
    stage.show();
}
}

控制器:

public class Controller {

@FXML private Button btnAdd;
@FXML private Button btnDelete;
@FXML private TreeTableView<String> treeTableView;
@FXML private TreeTableColumn<String, String> columnC1;

public void initialize() {
    final TreeItem<String> childNode1 = new TreeItem<>("Child Node 1");
    final TreeItem<String> childNode2 = new TreeItem<>("Child Node 2");
    final TreeItem<String> childNode3 = new TreeItem<>("Child Node 3");

    final TreeItem<String> root = new TreeItem<>("Root node");
    root.setExpanded(true);
    root.getChildren().setAll(childNode1, childNode2, childNode3);

    columnC1.setCellValueFactory((TreeTableColumn.CellDataFeatures<String, String> p) ->
    new ReadOnlyStringWrapper(p.getValue().getValue()));

    treeTableView.setRoot(root);
    treeTableView.setShowRoot(true);
    btnAdd.setOnAction(actionEvent -> root.getChildren().add(new TreeItem<>("Another Child")));
    btnDelete.setOnAction(actionEvent -> root.getChildren().remove(root.getChildren().size()-1));
}
}

文件格式:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
        prefWidth="450.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
    <TreeTableView fx:id="treeTableView" prefHeight="100.0" prefWidth="430.0" BorderPane.alignment="CENTER">
        <columns>
            <TreeTableColumn fx:id="columnC1" prefWidth="430.0" text="C1" />
        </columns>
    </TreeTableView>
</center>
<bottom>
    <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <children>
            <Button fx:id="btnAdd" mnemonicParsing="false" prefHeight="35.0" prefWidth="113.0" text="Add" />
            <Button fx:id="btnDelete" mnemonicParsing="false" prefHeight="35.0" prefWidth="95.0" text="Delete" />
        </children>
    </HBox>
</bottom>

我可以添加或删除行,这很好用。但是一旦右侧的滚动条出现并添加/删除行,而滚动条不在底部,TreeTableView 内容就会消失(如果底部有第二个滚动条,则内容不会消失)。向下滚动后它再次出现,所以这不是一个可怕的错误,而是一个烦人的错误。

有没有人解释这个错误甚至解决方案?

谢谢

编辑:

以下是 sunflame 要求的屏幕截图: 它工作到这里 直到我滚动内容才会消失 使用 JFX 15,即使滚动也会留下一团糟

我正在使用 java 8、jfx 11 我的操作系统是 Ubuntu 18.04.5 LTS (Bionic Beaver)

标签: javajavafxtreetableview

解决方案


推荐阅读