Results 1 to 2 of 2
  1. #1
    mcashe is offline Member
    Join Date
    Aug 2009
    Rep Power

    Default JTree Multi-Select

    I have a requirement to enable a multi select capability on a JTree. This is pretty easy to do with the DISCONTIGUOUS_TREE_SELECTION constant. The challenge with the requirement is that the user should not be allowed to multi select both parent and child nodes. Either all parent or all child nodes. JTree is a complicated component and I'm not sure how to tackle this one. Thanks for your thoughts.

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Pittsburgh, PA
    Rep Power


    It seems to me the real problem here is how to indicate to the user what is going on.
    Perhaps the first click determines whether selecting children or parents.
    But then how could the user change hisr mind?
    Maybe a radio button set for choosing between the two options.
    Experimentation may be useful.

    After the program knows what the user wants, it could highlight the selectable nodes.
    To do this, implement a CellRenderer.

    To restrict selection, intercept mouse hits and check the target before doing the selection.

    Here is a cellrenderer I used to indicate if nodes were selected, as defined by a bit in the node:
    Java Code:
    	static class RenderTag extends DefaultTreeCellRenderer {
    	    public Component getTreeCellRendererComponent(
                            JTree tree, Object value, boolean sel,
                            boolean expanded, boolean leaf,
                            int row, boolean hasFocus) {
    				tree, value, ((Tag)value).isSelected,
    				expanded, leaf, row, hasFocus);
    		return this;
    	} // end RenderTag
    The code intercepts mouse hits thus:
    Java Code:
    	// do my own mouse processing: expand, select, contract
    	MouseListener[] ml = getMouseListeners();
    	for (MouseListener l : ml)
    	addMouseListener (new ClickHandler());
    To allow discontiguous selection even in collapsed nodes, I needed to subclass JTree and override an internal method:
    Java Code:
    	protected boolean removeDescendantSelectedPaths(TreePath path,
    			boolean includePath) {
    		//TreePath[]    toRemove = getDescendantSelectedPaths(path, includePath);
    		//if (toRemove != null) {
    		// getSelectionModel().removeSelectionPaths(toRemove);
    		//    return true;
    		// }
    		return false;

    And here is an extract of clickhandler (as I extracted it, I may have messed it up, let's hope not)
    Java Code:
    class ClickHandler extends MouseAdapter implements ActionListener {
    	JMenuItem menuItemDelete;
    	TreePath selPath;
    	Tag currentTag;		// Tag where the mouse was clicked
    	ClickHandler() {}
    	public void mouseReleased(MouseEvent e) {
    		// by observation, the text of a node starts at depth*20
    		// clicks on icon will expand/contract
    		// clicks on text will select/deselect
    		boolean inText = e.getX() > currentTag.depth*20;
    		// expand/contract OR select/deselect
            	if ( ! inText && != null) {
    				// click on icon at front of non-leaf line: expand or contract 
    			if (isExpanded(selPath)) {
    				Enumeration<TreePath> xKids
    							= getExpandedDescendants(currentTag.getPath());
    				while (xKids.hasMoreElements())
    				if (isExpanded(selPath))
    			else {
    		else	{	// leaf or click in text:  toggle selection
    			 if (currentTag.isSelected)
    	} // end mouseReleased
    } // end class ClickHandler

Similar Threads

  1. JCheckbox in Jtree
    By shajuantony in forum AWT / Swing
    Replies: 10
    Last Post: 09-09-2009, 11:42 AM
  2. zip to JTree
    By icsbcn in forum AWT / Swing
    Replies: 8
    Last Post: 09-01-2009, 05:26 PM
  3. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 12:59 PM
  4. How to retain value in struts 2 using <s:select></s:select> tag
    By SaiPrasad@Sella in forum Web Frameworks
    Replies: 0
    Last Post: 02-09-2009, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts