Java JTree: only add child if there are no other childs - java

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);

Related

how to change the Node of a TriplePath in Jena?

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)

How to make child Tree Items checked/un-checked if parent Tree was made checked/un-checked

I'm having a Check-box Tree View structure, consisting of parent and child nodes.
I want to make all the child nodes [of the parent node] appear as checked if parent Tree is checked. Similarly, if parent Tree is unchecked then its childrens should be unchecked.
The best way to achieve this would be using the JFace CheckboxTreeViewer as it has the following predefined methods to simplify the task.
setSubtreeChecked - Sets the child elements checked on selecting the parent node
getCheckedElements - Gets all the checked tree elements
final CheckboxTreeViewer treeViewer = new CheckboxTreeViewer(parent);
// When user checks a checkbox in the tree, check all its children
treeViewer.addCheckStateListener(new ICheckStateListener() {
public void checkStateChanged(CheckStateChangedEvent event) {
// If the item is checked . . .
if (event.getChecked()) {
// . . . check all its children
treeViewer.setSubtreeChecked(event.getElement(), true);
}
}
});
// Get the selected elements from the tree
Object[] actuallyChecked = treeViewer.getCheckedElements();

Java: How to programmatically select and expand multiple nodes in a JTree?

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

make a duplicate node in Jtree

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.)

Populating JTree from database

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. :)

Categories