Finding the node where an instance belong - java

I generated a decision tree based on a set of data, then I converted this data to a xml file, after that I put it into a JTree. This part works fine but now I have to take a new instance(which contains the data received from the user), find it's place in the decision tree and change that node's color. And I can't find a way to do that. I am using weka and the J48 classifier to generate the decision tree. The xml is created in the Luc Sorel style: http://www.lucsorel.com/media/downloads/sample_decision_tree.xml
The first thing I tried was to classify the instance using the algorithm but that gets me only the class where it belongs, and I don't know how to locate it in the JTree.
What should I do? Any ideeas?

Like JTable, JTree uses a flyweight renderer to draw nodes. As the default renderer is a JLabel, you can set the foreground color or make the label opaque and set the background color. This related example changes the icon for emphasis. More examples may be found here.
Addendum: I cannot find … the node that I should color.
When getTreeCellRendererComponent() is called, value is a reference to the node to be rendered, and the tree parameter allows access to the TreeModel as a whole via getModel(). This example shows how to search a tree.

Related

How to remove tree lines from JideSoft TreeTable

I have created a tree using TreeTable and now that I have all the data in I want to remove the tree lines that are displayed on the left side.
As you can see here:
I want the tree to look like this:
I tried using these lines of code to remove them:
treeTable.setShowGrid(false);
treeTable.setShowHorizontalLines(false);
treeTable.setShowVerticalLines(false);
treeTable.setGridColor(Color.WHITE);
Is there some table property I am missing that can be disabled?
You can use TreeTable.setShowTreeLines() method:
We used the same +/- icons used in JTree so it will change based on
different LookAndFeels. You can also define your own icons by calling
CategorizedTable.setExpandedIcon(javax.swing.Icon) and
CategorizedTable.setCollapsedIcon(javax.swing.Icon). The tree lines
can be turn on or off using setShowTreeLines(boolean). The line color
can be set by setTreeLineColor(java.awt.Color). By default, it will
use the JTree's tree line color which is
UIManagerLookup.getColor("Tree.hash").

(JGraphX) How to add an extra label to a vertex?

I have a vertex and it has its name, the label inside the vertex. But I also wan't to add a new label, with extra information, for the same vertex. Is there a way to do it?
If you need only a "static label" you could use a "stencil".
The Stencils.java example program has some multi-label shapes.
If however you want more control on the content you want to show in the label, I could not find any "official" documentation: you might want to look at the examples I've added to a related question.
Here are the direct links to the examples:
An example of a multi-value node in JGraphX using "child nodes"
Here I've created child nodes and "locked" them to a parent/container node, by setting relative coordinates.
An example of a multi-value node in JGraphX using "custom shape"
Here I've created a "custom shape" to access the Graphics2D awt object and be able to draw my shape and labels.

JTree Drag and Drop: drag nodes with its childs

I would like to add drag and drop feature to my JTree.
I have tried to run example from this thread. It works but it works only on last leaves in tree, not on nodes that contain some childs. I have also looked on this thread.
I have defined canImport and importData methods, and also have set setDragEnabled(true). I just cannot drag nodes also with its childs.
How to edit example from first link to drag also nodes with its childs? As I understand correctly, I should set DataFormat instance, to set what types should be dragged.
Can anybody help?

How to create inverted JTree in Java?

Actually I'm interested to create an inverted JTree in Java in such a way that root exists at top and its child nodes at the next level and so on. As we know in case of simple JTree the child nodes appears expanding on the right side of the parent node but I want to implement inverted JTree in my project where child nodes expands downwards which gives an appearance of "TREE" type of data structure.
As this is very essential for my project so can anyone suggest me the code for the above mentioned problem?
Thanks in advance.
Then JTree is not the correct choice. Perhaps, you should look into Java2D and stuff like that.

JTree node labels change often but width is cached

Currently, I'm using a custom TreeCellRenderer to handle label and icon changes for my JTree. The problem comes when I need to change the text on a few nodes. The TreeCellRenderer works great in that the new text is displayed. However, the width from the initial text is cached and is not updated. I've attempted to hack it by overriding getPreferredSize in the TreeCellRenderer but that's not working properly since I have html strings. I've even tried parsing out the tags and getting the width, but it's still not perfect. I've used SwingUtilities, and FontMetrics.
It seems that using the tree's backing DefaultTreeModel.nodeChanged(TreeNode) is the proper way to change a node. However, I would need to have an additional data structure to handle accessing the nodes that need to be changed. Personally, I would like to only change the nodes that need changing. But, that would require another data structure to find all nodes that need to be updated kinda thing. What's the best way around the size caching of the backing JLabel in this situation?
I've found that using the way you suggested of calling nodeChanged has worked best for me.

Categories