i want to make a duplicate node in Jtree but the code is not working inside mouse action listener....
/* DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
def obj = selectedNode.getUserObject()
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)node.getRoot().getChildAt(0);
model.insertNodeInto(selectedNode, parentNode, 0)*/
I don't see a call to "new" anywhere in this code. Did I miss it? Wouldn't that be a requirement or creating a new Node?
Create a new DMTN and initialize it with the state of the one you want to copy.
You are not making a copy, you just try to insert the (existing) node into a different location.
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
def obj = selectedNode.getUserObject()
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)node.getRoot().getChildAt(0);
model.insertNodeInto(new DefaultMutableTreeNode(obj), parentNode, 0);
(Obvious syntax errors have not been corrected, I am not your compiler.)
Related
I have below Tree structure which is sub class of JTree
When the user selects a node, Time in image above, I need to get the Row index (index value = 3) of Demoscrigo(image above) with respect to Wells not from actual Root. Same index will be used to pre-select the row in other UI.I can get the selected Object but as the nodes has duplicate entries that is making things difficult.I have tried below options
getRowForPath(new TreePath(selectedNode.getPath()))
getSelectionRows()
this.getModel().getIndexOfChild(userObj, selectedNode);
JavaClientTreeModel treeModel = (JavaClientTreeModel) this.getModel();
selectedNode.getLevel()
this.getLastSelectedPathComponent();
this.getLeadSelectionRow();
But not able to figure out any specific combination. Please suggest me if any other solution is there.
First fetch the node for which Index need to be calculated by using the below code.
Parameter Type is used to get the above node
public DefaultMutableTreeNode findNode(DefaultMutableTreeNode node, String type){
Object data = (XXXX) node.getUserObject();
if(data.getType().equals(type)){
return node;
}else if(!node.isRoot()){
return findNode((DefaultMutableTreeNode) node.getParent(), type);
}
return null;
}
And then use
int rowIndex = node.getParent().getIndex(node);
I want to change a node of a Jena TriplePath (org.apache.jena.sparql.core.TriplePath), but I haven't found any manner. Imagine I have this code:
TriplePath tp = null;
....
//tp has been defined and not null
Node domain = tp.getSubject();
Node predicate = tp.getPredicate();
Node range = tp.getObject();
Node newNode = NodeFactory.createURI("http://www.example.com/example/example");
//And now? How can I set a Node (domain/predicate/range) of tp?
The question is, how can I set any Node (domain/predicate/range) of the TriplePath tp with the newNode I've created? Is there any manner?
You need to create a new path and assign it to tp. TriplePaths are immutable, as is the rest of the SPARQL algebra in Jena (any ways to defeat this should not be used!).
For more complex setups, have a template with variables and use:
TriplePath Substitute.substitute(TriplePath triplePath, Binding binding)
I have a JTree with a root and several nodes. When the user adds a node to another node with no children, the child is added. But when there are already nodes in the selected node, the node can't be added.
This is my code:
DefaultMutableTreeNode selectedNode = DefaultMutableTreeNode)treeExpertises.getSelectionPath().getLastPathComponent();
selectedNode.insert(new DefaultMutableTreeNode(newDomain), selectedNode.getChildCount());
I also tried this with the same result:
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)treeExpertises.getSelectionPath().getLastPathComponent();
selectedNode.add(new DefaultMutableTreeNode(newDomain));
I already found the answer:
DefaultTreeModel model = (DefaultTreeModel)treeExpertises.getModel();
model.nodeStructureChanged(selectedNode);
I have a JTree and an awt.Canvas. When i select multiple objects from within the Canvas into the objList, I want all the selected items to be shown inside the JTree as selected. That means for example if I have 2 objects selected, both their paths to root should be expanded, and also each selected object should have its corresponding TreeNode selected. My JTree has TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION.
Here is a sample of the expand funcion i use :
public void selectTreeNodes() {
HashMap <String, MyEntity> entities = ...;
Iterator it = entities.keySet().iterator();
while (it.hasNext()) {
String str = it.next().toString();
MyEntity ent = entities.get(str);
if (ent.isSelected()) {
DefaultMutableTreeNode searchNode = searchNode(ent.getName());
if (searchNode != null) {
TreeNode[] nodes = ((DefaultTreeModel) tree.getModel()).getPathToRoot(searchNode);
TreePath tpath = new TreePath(nodes);
tree.scrollPathToVisible(tpath);
tree.setSelectionPath(tpath);
}
}
}
}
public DefaultMutableTreeNode searchNode(String nodeStr)
{
DefaultMutableTreeNode node = null;
Enumeration enumeration= root.breadthFirstEnumeration();
while(enumeration.hasMoreElements()) {
node = (DefaultMutableTreeNode)enumeration.nextElement();
if(nodeStr.equals(node.getUserObject().toString())) {
return node;
}
}
//tree node with string node found return null
return null;
}
In my current state, if I select a single object, it will be selected in the JTree and its TreePath will be shown.
But if entities has more than 1 object selected, it will display nothing, my JTree will remain unchanged.
You are looking for the TreeSelectionModel of the JTree (use the getter). Use the TreeSelectionModel#setSelectionPaths for multiple paths. Now you are only setting one node selected due to your tree.setSelectionPath(tpath); call. The TreeSelectionModel also has methods to add/remove to an existing selection ,... (basically everything you might need in the future).
An interesting method for the expansion is the JTree#setExpandsSelectedPaths method which allows to configure the JTree to automatically expand selected paths. If you want to manage this manually, you can use the JTree#setExpandedState method
I have a table with fields category_id, category_name and parent_category_id. And parent_category_id has values from category_id which represents the parent child relationship. I dont have any fixed level of hierarchy, it may go up to 5 levels or 10 levels and there is no limit to that.. I need a code for how to implement this JTree to make thing work for me. I should be able to implement the same for Menu bar as well.. Please help me with this..
After googling I found this,
Map<String, Node> idToNode = new HashMap<String, Node>();
//create nodes from ResultSet
while ( resultSet.next() ){
Node node = //create node -contains info, parent id, and its own id from ResultSet
//put node into idToNode, keyed with its id
}
//link together
Iterator<String> it = idToNode.keySet().iterator();
Node root = null;
while ( it.hasNext() ){
Node node = idToNode.get(it.next());
Node parent = idToNode.get(node.getParentId());
if ( parent == null ) {
root = node;
}else{
parent.addChild(node);
}
}
How do i code those commented instructions?
Use DefaultMutableTreeNode to create your nodes
Make a map of IDs to nodes - as you get your nodes from the database, store them in the map with the id as their key.
Once you have all your nodes, go through them once more and match their parent ids up, retrieving them from the map.
Assuming your tree is structurally sound in the database, it will be sound here. Pick any node and follow the parent chain the the root.
With the root object, you can create your JTree. :)