Select all checkbox nodes in a jtree - java

I have a checkbox node tree build based on a named vector .
And i have a button called select all .
When i click the select all button , i need all the nodes on the chekbox node tree to be selected .
The code i have used is
for (CheckBoxNode rowNode: CheckBoxNodeTree. checkBoxCoulmn)
{
if(rowNode instanceof CheckBoxNode)
rowNode.setSelected((true));
}
Here checkBoxColumn is a arraylist which contains all the nodes of the tree as (Node , isSelected) .
But when i do this , nothing happens to the tree.

I've done it by casting the tree node to a default mutable tree node and get an enumeration of the children. Then you can iterate through them and setSelected(true). Your way could run into problems with threading or concurrent modifications if the user repeatedly clicks.
Enumeration<TreeNode> children = ((DefaultMutableTreeNode) node).breadthFirstEnumeration();
while (children.hasMoreElements()) {
TreeNode child = children.nextElement();
Object currentNode = ((DefaultMutableTreeNode) child).getUserObject();
//cast your currentNode to the check box and set selected or unselected.
}
Also, are you doing this on the event dispath thread? If not that might be why you aren't seeing any updates to the screen.

Related

Only handling tree will expand but not tree selection in JTree - Java Swing

I am doing lazy loading for my app. I want a node to load only user click to it's icon. The point is i don't know that node have it's children or not.My temporary solution is to define a node having children then loading them based on selection event, i don't use tree will expand event for lazy loading. Is there any ways for me to just implement treeWillExpand event. You can reference in TreeWillExpanListener and TreeExpandEventDemo2.
// Have a tree with some unexpanded root items
// When an item is expanded, add some children
tree.addListener(new Tree.ExpandListener() {
public void nodeExpand(ExpandEvent event) {
// No children for the first node
if (!hasChildren(event.getItemId())) {
tree.setChildrenAllowed(event.getItemId(), false);
} else {
// Add a few new child nodes to the expanded node
tree.addItem(childId);
tree.setParent(childId, event.getItemId());
}
}
});
you can implement hasChildren to find the child based on tree node being expanded and then find child and add

java swt jface TreeViewer expanding from node

How can I expand the node to its root node?
So I have this method to expand its parent node recursively
private void expand( Object object ) {
if ( object.getParent() != null ) {
tree.setExpandedState( object.getParent(), true );
expand( object.getParent() );
}
}
Use the expandToLevel TreeViewer method:
viewer.expandToLevel(element, 1);
element can be your model element (the object your content provider provides) or it can be a TreePath. You may need to call setUseHashlookup(true) on the viewer to speed up element lookup.

Primefaces Tree: don't remove the node after drag and drop

Is it possible not to remove the node from the original PrimeFacesTree after dragging it? The default behaviour is that a node that was dragged and dropped in another place is removed. Can i prevent that from happening?
I'm using Primefaces 4.0
There isn't any premade attribute to duplicate node on dropEvent.
The solution is to add a listener to your <p:tree> element :
<p:tree listener="#{managingBean.onDragDrop}" />
Then you need to re-create node on initial location by duplicating it in your backbean method :
public void onDragDrop(TreeDragDropEvent event) {
TreeNode dragNode = event.getDragNode();
TreeNode dropNode = event.getDropNode();
int dropIndex = event.getDropIndex();
// Logic to repopulate initial Tree element
}
And don't forget to re-draw your tree

change the jtree node text runtime

I am trying to create a JTree in java swing now i want to change the node text at runtime
try
{
int a=1,b=2,c=3;
DefaultMutableTreeNode root =
new DefaultMutableTreeNode("A"+a);
DefaultMutableTreeNode child[]=new DefaultMutableTreeNode[1];
DefaultMutableTreeNode grandChild[]= new DefaultMutableTreeNode[1];
child[0] = new DefaultMutableTreeNode("Central Excise"+b);
grandChild[0]=new DefaultMutableTreeNode("CE Acts: "+c);
child[0].add(grandChild[0]);
root.add(child[0]);
tree = new JTree(root);
}
catch(Exception ex)
{
ex.printStackTrace()
}
Now i want later on how can i change A 1 to a 2 dynamically and similarly in child and grand child nodes
You are looking for javax.swing.tree.DefaultMutableTreeNode.setUserObject(Object)
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
root.setUserObject("My label");
model.nodeChanged(root);
This assumes that you are using the DefautltTreeModel.
If you're not using a custom TreeModel, then the model of your tree is a DefaultTreeModel.
You'll need to walk the tree with some kind of comparator, given your DefaultMutableTreeNode getUserObject() (string or whatever) to achieve what you want.
You have 2 simple options accordingly to your question and the code that you pasted :
If your change is triggered by let's say a click event, you can get the selection and walk the tree from there.
Otherwise you'll need to walk the tree from the root
Upon successful changes, you'll need to fire events from the model that will trigger later a repaint of the view (nodesWereInserted, etc.).
Hope it helps

DefaultMutableTreeNode icon

I have a JTree displayed in a JContentPane built from DefaultMutableTreeNode objects. The tree is intented to display the local file-system. The data should be loaded on request, therefore when the user wants to expand it. This works well, but as long as there are no child items in the node, it displays a file-icon, and turns into a folder-icon when child items have been inserted.
How can I make the node display a folder-icon always, although there are (yet) no child items?
You need to implement cell renderer for your tree. So you can define icon for the node.
See here the sample for a table (tree also has method setCellRenderer)
Using a DefaultMutableTreeNode (or a custom implementation of TreeNode), the means to distinguish files from empty folders is its allowsChildren property:
// get a list of files
File[] files = new File(".").listFiles();
// configure the nodes' allowsChildren as the isDir of the File object
for (File file : files) {
root.add(new DefaultMutableTreeNode(file, file.isDirectory()));
}
// configure the TreeModel to use nodes' allowsChildren property to
// decide on its leaf-ness
DefaultTreeModel model = new DefaultTreeModel(root, true);
I use this:
DefaultMutableTreeNode root = new DefaultMutableTreeNode ();
DefaultTreeModel treeModel = new DefaultTreeModel (root);
tree = new JTree (treeModel);
addFiles (root); // build the catalog tree recursively
treeModel.setAsksAllowsChildren (true); // allows empty nodes to appear as folders
with
if (file.isDirectory ())
newNode.setAllowsChildren (true);
in the addFiles() routine. The setAsksAllowsChildren(true) needs to come after the tree is built.

Categories

Resources