Populating JTree from database - java

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

Related

Java Swing How to get child node row index with respect to other child node

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

How to create dynamic tree data structure in Java

specifically I need to represent the following:
The tree at any node can have an arbitrary number of children
Each parent node (after the root) is just a String (whose children are also Strings)
I need to be able to get parent and list out all the children (some sort of list or array of Strings) given an input string representing a given node
Dynamically populating the tree structure based on reference relationship between parent and child.
Example given is I have one member1 sponsor another member2, and member2 sponsor member 3 and so and so for. Already have the table records relationship
Is there an available structure for this ???
My data is from DB or a List, I will loop through the information with the name and the relation to determine if the node is a root, parent or a child.
So during the loop, I found a child, I need a reference to the parent so that I can compare the child relation to the parent before adding the child to its parent.
The closest code I found .
public class TreeNode<T> implements Iterable<TreeNode<T>> {
T data;
TreeNode<T> parent;
List<TreeNode<T>> children;
public TreeNode(T data) {
this.data = data;
this.children = new LinkedList<TreeNode<T>>();
}
public TreeNode<T> addChild(T child) {
TreeNode<T> childNode = new TreeNode<T>(child);
childNode.parent = this;
this.children.add(childNode);
return childNode;
}
// other features ...
}
Sample usage:
TreeNode<String> root = new TreeNode<String>("root");
{
TreeNode<String> node0 = root.addChild("node0");
TreeNode<String> node1 = root.addChild("node1");
TreeNode<String> node2 = root.addChild("node2");
{
TreeNode<String> node20 = node2.addChild(null);
TreeNode<String> node21 = node2.addChild("node21");
{
TreeNode<String> node210 = node20.addChild("node210");
}
}
}
This is what I have done so far. The parent will get overwritten by the latest entry so hence I am unable to retrieve what I have added previously .
public static TreeNode<String> getSet1() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] items = line.split(":");
String name = items[0];
String parent = items[1];
String type = items[2];
if (parent.equalsIgnoreCase("-") && type.equalsIgnoreCase("mainparent")) {
root = new TreeNode<String>(name);
} else if (type.equalsIgnoreCase("ChildParent") && parent.equalsIgnoreCase(root.toString())) {
childParent = root.addChild(name);
} else if (type.equalsIgnoreCase("Child") && parent.equalsIgnoreCase(childParent.toString())) {
child = childParent.addChild(name);
}
}
return root;
}
Your diagram indicates a tree of arbitrary depth, but your code only handles grandparent -> parent -> child relationships (with a single grandparent at the root).
I would ignore the type, as all you need is the name of a person and the name of their parent. If the parent name is a dash, you know you have the root.
Now for each person, you need to get the parent node already in the tree (assuming parents come before children in the list - if that's not the case, the problem becomes significantly more complex, as you would have to store orphaned persons temporarily and for each new person see if they are the parent of an orphaned person).
In order to get the parent by name, you should store each person you have already processed in a second data structure, parallel to the tree. The second data structure should make it easy to look someone up by name. Maps, and in particular Hashtables, are ideal for this. This is how it works:
Map processedPersonsMap=new Hashtable<String, TreeNode<String>>();
For each person, you store them in the map, indexed by their name:
TreeNode<String> person=...;
processedPersonsMap.put(person.getData(), person);
When you read in a new person and their parent's name is not a dash, you look up the parent:
String parentName=items[1];
TreeNode<String> parent=processedPersonsMap.get(parentName);
In this way, no matter how deep the tree is, you will always find the right parents. However, keep in mind that this requires a valid input file where each child comes after their parent, and which does not contain circular references or missing parents.
If those conditions are not met, you have to handle them explicitly.

How can I get a TreeView branch by name rather than value?

Is there any way to get a TreeView branch by name?
For example, if I have a TreeView menu like so:
TreeItem<String> root, branch;
root = new TreeItem<>();
root.setExpanded(true);
branch = makeBranch("Chicken", root);
makeBranch("Hen", branch);
Here is the makeBranch() method:
public TreeItem<String> makeBranch(String s, TreeItem<String> parent){
TreeItem<String> item = new TreeItem<>(s);
item.setExpanded(true);
parent.getChildren().add(item);
return item;
}
Now if I want to get a branch, I can do:
branch.getChildren().get(a_number); // get using index
But is there anyway I can get a branch by its name rather than value?
So something like:
branch.getChildren().get("the name of leaf or branch");
Since TreeItem.getChildren() returns a ObservableList<TreeItem<T>>, you can use the methods of List to find the child you're looking for, e.g. using Stream to filter the content:
final String value = "the name of leaf or branch";
Optional<TreeItem<String>> nodeOptional = branch.getChildren().stream()
.filter(
(child)-> child.getValue().equals(value)
)
.findFirst();
if (nodeOptional.isPresent()) {
TreeItem<String> item = nodeOptional.get();
// do something with item
} else {
// no child with specified value was found
}
Note that you only search the direct children of branch that way. If you want to find arbitrary descendants of branch, you have to do a tree search.

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

Adjacency List HashMap<String, ArrayList<Edge>> failing to locate its values

I am halfway through debugging a Breadth-First Search algorithm using an adjacency list representation of graph data: HashMap<String, ArrayList<Edge>>. Each String key is the name of an underground station, and each ArrayList is a list of the edges for that station.
I am using a queue to store nodes of the graph in the order they are traversed. So I inspect the next in the queue for it's child's name. I then want to get the child's ArrayList of edges from the adjacencyList by using something like childEdges = stationsAdjacencyList.get(childNodeName);.
My syntax is a little different, but please check the code below.
At the moment the .get() function is not returning an ArrayList but is returning null every time instead. I know that the HashMap lookup is receiving the correct Key. It is just refusing to give me any value from it's associated bucket.
while (!q.empty()) { //
String endpointName; // the Key part for the next node lookup
// get next node (single entry of adjacency list)
Map<String, ArrayList<Edge>> currentNode = (Map<String, ArrayList<Edge>>) q.deque();
HashMap<String, ArrayList<Edge>> nextNode = new HashMap<String, ArrayList<Edge>>();
for (Map.Entry<String, ArrayList<Edge>> node : currentNode.entrySet()) { // there is only one node
++levelCount; // next node iteration is one level down the tree
for (Edge edge : node.getValue()) { // for each of this nodes Edges
endpointName = edge.getEndpoint(); // retrieve the name of adjacent
if (!endpointName.equals(destination)) { // if it's not the destination
levelTracker.put(edge.getParent(), levelCount); // record the level in the tree of this node
ArrayList<Edge> nextNodeEdges = adjacencyList.get(endpointName);
nextNode.put(endpointName, nextNodeEdges); // create child node from endpoint
q.enqueue(nextNode); // add child to queue
}
else if (endpointName.equals(destination)) { // if we're done
path.add(endpointName); // record the destination in the path (reverse order)
getPathBack(edge, levelCount + 1); // + 1 levelCount to indicate destination level in tree
break;
}
}
}
}
Apologies if the code is not so clean or with decent comments, it is changing constantly. Hopefully someone can tell me why the ArrayList<Edge> nextNodeEdges = adjacencyList.get(endpointName); is not fetching anything.
Thanks!!
So a good test is to see if calling adjacencyList.get("valid endpoint"); in the same spot with a hardcoded value will return a non-null list. If it doesn't then adjacencyList is getting destroyed somewhere, if it does then endpointName isn't as correct as you think it is.

Categories