JavaFX TreeView:setGraphic()用于不同级别的treeItem [英] JavaFX TreeView: setGraphic() for different levels of treeItem
问题描述
我正在创建一个TreeView,它应该为不同级别的树结构提供不同的图形。这是一个三级结构,其中隐藏了根。
可扩展节点的图形:
http://i.imgur.com/wv00CEi.png
叶子节点:$ b
$ b
只是一个hBox中的标签。
这是我迄今为止所尝试的,但是我得到一个nullPointerException异常,基本上说getTreeView为null:
CustomTreeCellFactory
public final class CustomTreeCellFactory扩展TreeCell< String> {
private TextField textField;
私人HBox hBox;
public CustomTreeCellFactory(){
super();
if(getTreeView()== null){
System.out.println(Her er problem);
}
if(getTreeView()。getTreeItemLevel(getTreeItem())== 1){
try {
hBox =(HBox)FXMLLoader.load(getClass()。getResource ( /Views/TreCell.fxml));
} catch(IOException e){
System.out.println(This did not work);
e.printStackTrace();
else if(getTreeView()。getTreeItemLevel(getTreeItem())== 2){
try {
hBox =(HBox)FXMLLoader.load (的getClass()的getResource( /查看/ TreCellLowestLevel.fxml)。);
} catch(IOException e){
System.out.println(This did not work);
e.printStackTrace();
}
}
}
设置Cell Factory的代码片段
TreeView< String> tree =(TreeView)parent.getChildren()。get(0);
tree.setRoot(root);
tree.setShowRoot(false);
tree.setEditable(true);
tree.setCellFactory(new Callback< TreeView< String>,TreeCell< String>>(){
@Override
public TreeCell< String> call(TreeView< String> param) b $ b返回新的CustomTreeCellFactory();
}
});
我发现问题所在。
在设置TreeView时,我试图做的工作需要在更新方法中完成。
这里是解决了这个问题的代码:
$ $ $ $ $ $ $ $ $ $ $ public $ {
$ public $ {
$ {
hBox =(HBox)FXMLLoader.load(getClass()。getResource(/ Views / TreCell.fxml));
} catch(IOException e){
System.out.println(This did not work);
e.printStackTrace();
try {
hBoxLeaf =(HBox)FXMLLoader.load(getClass()。getResource(/ Views / TreCellLowestLevel.fxml));
} catch(IOException e){
System.out.println(This did not work);
e.printStackTrace();
}
}
更新方法
@Override
public void updateItem(String item,boolean empty){
super.updateItem (项目,空);
if(item!= null){
if(getTreeView()。getTreeItemLevel(getTreeItem())== 1){$ b $ setGraphic(this.hBox);
} else if(getTreeView()。getTreeItemLevel(getTreeItem())== 2){
setGraphic(this.hBoxLeaf);
}
} else {
setGraphic(null);
}
}
I'm creating a TreeView that should have different graphics for different levels of the treestructure. It is a 3-level structure, where the root is hidden.
The graphic for expandable nodes: http://i.imgur.com/wv00CEi.png
The graphic for leaf nodes:
Just a label in an hBox.
This is what I have tried so far, but I get a nullPointerException basically saying getTreeView is null:
CustomTreeCellFactory
public final class CustomTreeCellFactory extends TreeCell<String>{
private TextField textField;
private HBox hBox;
public CustomTreeCellFactory(){
super();
if (getTreeView()==null){
System.out.println("Her er problem");
}
if (getTreeView().getTreeItemLevel(getTreeItem())==1){
try {
hBox = (HBox) FXMLLoader.load(getClass().getResource("/Views/TreCell.fxml"));
} catch (IOException e) {
System.out.println("This didn't work");
e.printStackTrace();
}
}
else if (getTreeView().getTreeItemLevel(getTreeItem())==2){
try {
hBox = (HBox) FXMLLoader.load(getClass().getResource("/Views/TreCellLowestLevel.fxml"));
} catch (IOException e) {
System.out.println("This didn't work");
e.printStackTrace();
}
}
}
Code snippet from where I set the Cell Factory
TreeView<String> tree = (TreeView) parent.getChildren().get(0);
tree.setRoot(root);
tree.setShowRoot(false);
tree.setEditable(true);
tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
@Override
public TreeCell<String> call(TreeView<String> param) {
return new CustomTreeCellFactory();
}
});
I found out what the problem was.
What I tried to do needed to be done in the update-method, when the TreeView is set.
Here's the code that solved the problem:
**Constructor**
public CustomTreeCellFactory(){
try {
hBox = (HBox) FXMLLoader.load(getClass().getResource("/Views/TreCell.fxml"));
} catch (IOException e) {
System.out.println("This didn't work");
e.printStackTrace();
}
try {
hBoxLeaf = (HBox) FXMLLoader.load(getClass().getResource("/Views/TreCellLowestLevel.fxml"));
} catch (IOException e) {
System.out.println("This didn't work");
e.printStackTrace();
}
}
Update Method
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
if (getTreeView().getTreeItemLevel(getTreeItem())==1) {
setGraphic(this.hBox);
}else if (getTreeView().getTreeItemLevel(getTreeItem())==2){
setGraphic(this.hBoxLeaf);
}
} else {
setGraphic(null);
}
}
这篇关于JavaFX TreeView:setGraphic()用于不同级别的treeItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!