我是java fx的新手。 我正在使用treeview创建一个应用程序,并且通过嵌套集模型进行树视图。所以每个treeitem必须有左,右数据。(我做了一个像Treeitem>的树项目。对象有数据(id,标签名称,左,右等),String是要在treeview上显示的标签名称。)
问题是当我添加新的treeitem(或删除treeitem)时,必须更新整个treeitems。我已经实现了要在DB上更新的整个数据(其他treeitem),但是treeview仍然没有更新数据。
我找了刷新treeview的方法,但我发现用户不应该强行使用updateitem。
有没有办法或方法刷新整个树视图?
FYI。代码
Pair<LabelTreeItem,String> f = new Pair<>(new LabelTreeItem(1, "Root", 0, 0, "",0), "Root");
try {
f = new Pair<>(service.getRoot(), "Root");
} catch (SQLException e) {
System.out.println("Can't find Root Node");
e.printStackTrace();
}
TreeItem<Pair<LabelTreeItem,String>> root = createNode(f);
root.setExpanded(true);
treeView.setRoot(root);
treeView.setEditable(true);
treeView.setCellFactory(new Callback<TreeView<Pair<LabelTreeItem,String>>,TreeCell<Pair<LabelTreeItem,String>>>(){
@Override
public TreeCell<Pair<LabelTreeItem,String>> call(TreeView<Pair<LabelTreeItem,String>> p) {
return new TextFieldTreeCellImpl();
}
});
.....................................
private TreeItem<Pair<LabelTreeItem,String>> createNode(final Pair<LabelTreeItem,String> f) {
return new TreeItem<Pair<LabelTreeItem,String>>(f) {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
@Override public ObservableList<TreeItem<Pair<LabelTreeItem,String>>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
@Override public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
Pair<LabelTreeItem,String> f = getValue();
int nodeLeft = f.getKey().getLeft();
int nodeRight = f.getKey().getRight();
if(nodeRight-nodeLeft == 1) {
isLeaf = true;
System.out.println("this is leaf");
}
}
return isLeaf;
}
private ObservableList<TreeItem<Pair<LabelTreeItem,String>>> buildChildren(TreeItem<Pair<LabelTreeItem,String>> TreeItem) {
Pair<LabelTreeItem,String> f = TreeItem.getValue();
System.out.println("ID: "+f.getKey().getId());
int nodeLeft = f.getKey().getLeft();
int nodeRight = f.getKey().getRight();
try {
if (f != null && nodeRight-nodeLeft != 1) {
ArrayList<LabelTreeItem> item = new ArrayList<>();
item = service.getChildren(f.getKey().getLeft(), f.getKey().getRight(), f.getKey().getDepth());
ArrayList<Pair<LabelTreeItem,String>> itemList = new ArrayList<>();
for(int i=0; i<item.size(); i++) {
System.out.println("test : " + item.get(i).getLabel());
Pair<LabelTreeItem,String> pair = createPair(item.get(i), item.get(i).getLabel());
itemList.add(pair);
}
if(itemList.size() != 0) {
ObservableList<TreeItem<Pair<LabelTreeItem,String>>> children = FXCollections.observableArrayList();
for (Pair<LabelTreeItem,String> childNode : itemList) {
children.add(createNode(childNode));
}
return children;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return FXCollections.emptyObservableList();
}
};
}
private Pair<LabelTreeItem, String> createPair(LabelTreeItem item, String name) {
return new Pair<>(item, name);
}
LabelTreeItem.java
public class LabelTreeItem {
private int id;
private String label;
private int left;
private int right;
private String comment;
private int depth;
public LabelTreeItem(int id, String label, int left, int right, String comment, int depth) {
this.id = id;
this.label = label;
this.left = left;
this.right = right;
this.comment = comment;
this.depth = depth;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getLeft() {
return left;
}
public void setLeft(int left) {
this.left = left;
}
public int getRight() {
return right;
}
public void setRight(int right) {
this.right = right;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
}
TextFieldTreeCellImpl.java
public TextFieldTreeCellImpl() {
service = SqliteService.getInstance();
MenuItem addLabel = new MenuItem("Label add");
addMenu.getItems().add(addLabel);
addLabel.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
getTreeItem().setExpanded(true);
LabelTreeItem newItem = null;
try {
id = getTreeItem().getValue().getKey().getId();
label = getTreeItem().getValue().getKey().getLabel();
depth = getTreeItem().getValue().getKey().getDepth()+1;
left = getTreeItem().getValue().getKey().getLeft();
right = getTreeItem().getValue().getKey().getRight();
newItem = service.getData("New Label", id,depth);
TreeItem<Pair<LabelTreeItem,String>> newLabel = new TreeItem(createPair(newItem, newItem.getLabel()));
getTreeItem().getChildren().add(newLabel);
} catch (SQLException e) {
e.printStackTrace();
}
}
});
MenuItem editLabel = new MenuItem("Label modify");
addMenu.getItems().add(editLabel);
editLabel.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
startEdit();
}
});
MenuItem removeLabel = new MenuItem("Label delete");
addMenu.getItems().add(removeLabel);
removeLabel.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
left = getTreeItem().getValue().getKey().getLeft();
right = getTreeItem().getValue().getKey().getRight();
service.deleteData(left, right, (right-left)+1);
} catch (SQLException e) {
e.printStackTrace();
}
getTreeItem().getParent().getChildren().remove(getTreeItem());
System.out.println("Remove");
}
});
}
@Override
protected void updateItem(final Pair<LabelTreeItem, String> pair, boolean empty) {
super.updateItem(pair, empty);
if (!empty && pair != null) {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
} else {
setText(pair.getKey().getLabel());
setGraphic(getTreeItem().getGraphic());
setContextMenu(addMenu);
}
} else {
setText(null);
setGraphic(null);
}
setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(pair !=null){
Tooltip tTip = new Tooltip(pair.getKey().getLabel());
tTip.uninstall(getTreeView(), tTip);
}
}
});
setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(pair !=null){
Tooltip tTip = new Tooltip();
tTip.setText(pair.getKey().getLabel()
+"\nComment : "+pair.getKey().getComment()
+"\nLeft : "+pair.getKey().getLeft()
+"\nRight : "+pair.getKey().getRight());
tTip.setFont(new Font("Arial", 16));
tTip.setStyle("-fx-background-color: grey;");
tTip.centerOnScreen();
tTip.install(getTreeView(), tTip);
}
}
});
}
数据库表
id|label|left|right|comment|depth