我有一个 JavaFX TreeTableView。在其根下,有一组 TreeItem(节点),每个节点可能有自己的子 TreeItem(子节点)。由于没有信息显示任何没有子节点的节点,我想删除这些。但不知何故,简单的删除操作有时会改变选择。
package sample;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
TreeItem<String> rootNode = new TreeItem<String>("Root");
rootNode.setExpanded(true);
rootNode.getChildren().setAll(new TreeItem<>("Node 0"),
new TreeItem<>("Node 1"), new TreeItem<>("Node 2"),//);
new TreeItem<>("Node 3"), new TreeItem<>("Node 4"));
for (int i = 0; i < 2; i++) {
TreeItem<String> node = rootNode.getChildren().get(i);
node.setExpanded(true);
node.getChildren().setAll(new TreeItem<>("Sub Node " + i + "-0"),
new TreeItem<>("Sub Node " + i + "-1"),
new TreeItem<>("Sub Node " + i + "-2"));
}
TreeTableColumn<String, String> column = new TreeTableColumn<>("Nodes");
column.setCellValueFactory((TreeTableColumn.CellDataFeatures<String, String> p) -> {
return new ReadOnlyStringWrapper(p.getValue().getValue());
});
column.setPrefWidth(200);
TreeTableView<String> table = new TreeTableView<>(rootNode);
table.getColumns().add(column);
int selectIndex = 4;
int removeIndex = 3;
table.getSelectionModel().select(selectIndex);
System.out.println("Selected index = " + table.getSelectionModel().getSelectedIndex());
System.out.println("Selected item = " + table.getSelectionModel().getSelectedItem().getValue());
table.getRoot().getChildren().remove(removeIndex);
System.out.println("Selected index = " + table.getSelectionModel().getSelectedIndex());
System.out.println("Selected item = " + table.getSelectionModel().getSelectedItem().getValue());
primaryStage.setTitle("Tree Table View Selection");
primaryStage.setScene(new Scene(table, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
代码原样产生以下输出:
Selected index = 4
Selected item = Sub Node 0-2
Selected index = 3
Selected item = Sub Node 0-1
但是,如果 (selectedIndex, removeIndex) 更改为 (3,4),则输出变为:
Selected index = 3
Selected item = Sub Node 0-1
Selected index = 3
Selected item = Sub Node 0-1
所选索引没有变化。
事实上,在我有限的测试案例中,只要 selectedIndex 小于或等于 removeIndex,那么 remove 操作不会改变选择。否则它会改变选择。
为什么会这样?有没有办法解决这个问题?