🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #831

Re: How to UnSelect a JTree?

#831
From: Stephan van Loendersloot
Date: Mon, 02 Aug 2004 16:37
118 lines
4483 bytes

Ramon F Herrera wrote:
> 
> I have a problem with JTrees: the user can click away
> selecting a set of paths (with the usual <shift> and
> <control> keys) but there is no click method that will
> restore the tree to its original, pristine, unselected
> state.
> 
> What I would like to implement is the familiar Mac and
> Windows icon selection: when the user clicks on the
> desktop all the file icons become unselected.
> 
> I figured out how to perform the actual unselection:
> 
>     rightTree.setSelectionPath(null);
> 
> However, I don't know what event should trigger the above
> response.  The only Tree Listener seems to be 'valueChanged'
> which is fired only when one of the folders is clicked.
> 
> How can I detect a click on the background of a tree??

Examine the snippet below.


------------TreeDemo.java------------

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class TreeDemo extends JFrame implements MouseListener,
         TreeWillExpandListener {
     JTree tree;

     boolean isExpanding = false;

     public TreeDemo() {
         super();

         DefaultMutableTreeNode nodeRoot = new 
DefaultMutableTreeNode("Folder1");
         DefaultMutableTreeNode nodeFolder1 = new DefaultMutableTreeNode(
                 "Folder1");
         DefaultMutableTreeNode nodeFolder2 = new DefaultMutableTreeNode(
                 "Folder2");
         DefaultMutableTreeNode nodeItem1 = new 
DefaultMutableTreeNode("Item1");
         nodeRoot.add(nodeFolder1);
         nodeFolder1.add(nodeFolder2);
         nodeFolder2.add(nodeItem1);
         tree = new JTree(nodeRoot);
         tree.addMouseListener(this);
         tree.addTreeWillExpandListener(this);

         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setSize(300, 300);
         this.getContentPane().setLayout(new BorderLayout());
         this.getContentPane().add(BorderLayout.CENTER, tree);
         this.setVisible(true);

     }

     public static void main(String[] args) {
         new TreeDemo();
     }

     public void mouseClicked(MouseEvent e) {
         if (e.getSource().equals(tree)) {
             TreePath path = tree.getPathForLocation(e.getX(), e.getY());
             if (path != null) {
                 DefaultMutableTreeNode clickedNode = 
(DefaultMutableTreeNode) path
                         .getLastPathComponent();
                 System.out.println(clickedNode.toString());
             } else {
                 if (!isExpanding) {
                     tree.clearSelection();
                 }
             }
             isExpanding = false;
         }
     }

     public void mouseEntered(MouseEvent e) {
     }

     public void mouseExited(MouseEvent e) {
     }

     public void mousePressed(MouseEvent e) {
     }

     public void mouseReleased(MouseEvent e) {
     }

     public void treeWillCollapse(TreeExpansionEvent event) {
         isExpanding = true;
     }

     public void treeWillExpand(TreeExpansionEvent event) {
         isExpanding = true;
     }

}

------------------------------------------------

     --Stephan.
Technical Details
Message-ID: <410e6de4@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41031939$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,2aac51bc857db07c
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Mon, 02 Aug 2004 18:37:57 +0200
From: Stephan van Loendersloot <stephan@DESPAMinfacto.nl>
User-Agent: Mozilla Thunderbird 0.7.1 (OS/2/20040701)
X-Accept-Language: nl,en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: How to UnSelect a JTree?
References: <41031939$1@newsgroups.borland.com>
In-Reply-To: <41031939$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 213.51.219.113
Message-ID: <410e6de4@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1091464676 213.51.219.113 (2 Aug 2004 09:37:56 -0700)
Lines: 118
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail