Java 动态添加行时,TreeTableView内容消失
我有一个小示例代码: 主要类别:Java 动态添加行时,TreeTableView内容消失,java,javafx,treetableview,Java,Javafx,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 Applicat
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));
}
}
公共类控制器{
@FXML专用按钮btnAdd;
@FXML专用按钮btnDelete;
@FXML私有树视图树视图;
@FXML私有树TableColumnC1;
公共无效初始化(){
final TreeItem childNode1=新TreeItem(“子节点1”);
final TreeItem childNode2=新TreeItem(“子节点2”);
final TreeItem childNode3=新TreeItem(“子节点3”);
最终树项根=新树项(“根节点”);
root.setExpanded(true);
root.getChildren().setAll(childNode1、childNode2、childNode3);
columnC1.setCellValueFactory((TreeTableColumn.CellDataFeatures p)->
新的ReadOnlyStringWrapper(p.getValue().getValue());
treeTableView.setRoot(root);
treeTableView.setShowRoot(true);
btnAdd.setOnAction(actionEvent->root.getChildren().add(newtreeitem(“另一个子”));
btnDelete.setOnAction(actionEvent->root.getChildren().remove(root.getChildren().size()-1));
}
}
fxml:
我可以添加或删除行,这很好。但是,只要右侧的滚动条出现,我添加/删除行,而滚动条不在底部,TreeTableView内容就会消失(如果底部有第二个滚动条,则内容不会消失)。向下滚动后,它再次出现,因此这不是一个可怕的错误,而是一个令人讨厌的错误
有人解释过这个错误,甚至有解决方案吗
谢谢
编辑:
以下是sunflame要求的屏幕截图:
我使用的是Java8,JFX11,我的操作系统是Ubuntu18.04.5LTS(仿生海狸)注意
Controller=newcontroller();装载机。设置控制器(控制器)代码>是不必要的,很可能没有效果。由于FXML文件中的fx:controller
属性,已经为您创建了一个实例。您所说的内容消失到底是什么意思?我已经测试了你的代码,它对我来说很好。我可以毫无问题地添加/删除行,这样行/文本不会消失。你能解释一下还是用截图展示一下?嗯。。WorkForme(fx15+,不包括fxml内容,它只是一个简单的树型表,带有一个按钮:)-可能是早期版本中的一个bug(尽管不记得任何类似于您的描述),或者:您确定这段代码复制了它吗?发现了一个可能相关的问题:-这是fx15+隐式修复的(这是当前的开发)通过修复VirtualFlow。sympton(空白视口)是相同的,只是与链接相关的方法不同(此处添加,此处折叠)是此链接:看起来这可能是我出现问题的原因。因此,希望这将在JFX16中得到修复
<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>