javafx中怎么手动的触发事件,如树节点的更新事件
1个回答
展开全部
实现方式:
可以实现采用手动触发事件的方式。
TreeItem<AbstractTreeNode> currentTreeItem = ****;//当前发生修改的树节点
currentTreeItem.getValue().setNodeText("abc");
//自动触发修改的事件,使树节点的显示信息能同步修改
Event.fireEvent(currentTreeItem, new TreeItem.TreeModificationEvent<AbstractTreeNode>(TreeItem.valueChangedEvent(), currentTreeItem, currentTreeItem.getValue()));
构建树节点的显示时自定义渲染方式:
TreeView<AbstractTreeNode> leftNodeTree = new TreeView<AbstractTreeNode>();
....
leftNodeTree.setCellFactory(new Callback<TreeView<AbstractTreeNode>, TreeCell<AbstractTreeNode>>() {
@Override
public TreeCell<AbstractTreeNode> call(TreeView<AbstractTreeNode> param) {
return new TreeCellImpl();
}
});
public class TreeCellImpl extends TreeCell<AbstractTreeNode> {
@Override
public void updateItem(AbstractTreeNode item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(item.getNodeText());
setGraphic(item.getGraphic());
if (item.getNodeType().equals(NodeType.TREE_CONFIG_NODE)) {
setContextMenu(new TreeConfigContextMenu(this.getTreeView(), this.getTreeItem(), item));
} else if (item.getNodeType().equals(NodeType.TREE_DETAIL_CONFIG_NODE)) {
setContextMenu(new TreeDetailConfigContextMenu(this.getTreeView(), this.getTreeItem(), item));
} else if (item.getNodeType().equals(NodeType.SHAREDATA_FIELDS_NODE)) {
// setContextMenu(new TreeConfigContextMenu(item));
}
}
}
}
class AbstractTreeNode {
private String nodeId;
private String nodeText;
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public String getNodeText() {
return nodeText;
}
public void setNodeText(String nodeText) {
this.nodeText = nodeText;
}
可以实现采用手动触发事件的方式。
TreeItem<AbstractTreeNode> currentTreeItem = ****;//当前发生修改的树节点
currentTreeItem.getValue().setNodeText("abc");
//自动触发修改的事件,使树节点的显示信息能同步修改
Event.fireEvent(currentTreeItem, new TreeItem.TreeModificationEvent<AbstractTreeNode>(TreeItem.valueChangedEvent(), currentTreeItem, currentTreeItem.getValue()));
构建树节点的显示时自定义渲染方式:
TreeView<AbstractTreeNode> leftNodeTree = new TreeView<AbstractTreeNode>();
....
leftNodeTree.setCellFactory(new Callback<TreeView<AbstractTreeNode>, TreeCell<AbstractTreeNode>>() {
@Override
public TreeCell<AbstractTreeNode> call(TreeView<AbstractTreeNode> param) {
return new TreeCellImpl();
}
});
public class TreeCellImpl extends TreeCell<AbstractTreeNode> {
@Override
public void updateItem(AbstractTreeNode item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(item.getNodeText());
setGraphic(item.getGraphic());
if (item.getNodeType().equals(NodeType.TREE_CONFIG_NODE)) {
setContextMenu(new TreeConfigContextMenu(this.getTreeView(), this.getTreeItem(), item));
} else if (item.getNodeType().equals(NodeType.TREE_DETAIL_CONFIG_NODE)) {
setContextMenu(new TreeDetailConfigContextMenu(this.getTreeView(), this.getTreeItem(), item));
} else if (item.getNodeType().equals(NodeType.SHAREDATA_FIELDS_NODE)) {
// setContextMenu(new TreeConfigContextMenu(item));
}
}
}
}
class AbstractTreeNode {
private String nodeId;
private String nodeText;
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public String getNodeText() {
return nodeText;
}
public void setNodeText(String nodeText) {
this.nodeText = nodeText;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询