🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
3 messages
3 total messages Started by Ramon F Herrera Sun, 25 Jul 2004 02:21
How to UnSelect a JTree?
#822
Author: Ramon F Herrera
Date: Sun, 25 Jul 2004 02:21
23 lines
1722 bytes
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??

TIA,

-Ramon F Herrera
Re: How to UnSelect a JTree?
#829
Author: John McGrath [TeamB]
Date: Mon, 02 Aug 2004 07:44
25 lines
1776 bytes
On 7/24/2004 at 10:21:42 PM, Ramon F Herrera wrote:

> 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??

JTree is derived from JComponent and Component, so it inherits all of
their methods.  If you check Component, you will find the
addMouseListener() method.

You will need to add a MouseListener and respond to one of the MouseEvents
(mousePressed, mouseReleased, mouseClicked), and determine if the mouse
position is pointing to one of the JTree's nodes.  I think you should be
able to use the JTree getRowForLocation() method to do that.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: How to UnSelect a JTree?
#831
Author: 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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads