Home arrow static arrow Java Programming [Archive] - Different action performed for JTree
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Different action performed for JTree
This topic has 2 replies on 1 page.

Posts:254
Registered: 4/15/04
Different action performed for JTree  
Jun 21, 2004 4:57 AM



 
I would like a different action to be performed dependant on whether the user clicks the +/- icon or the display name for the node. How do I do that?

Currently, I have it set that if a user clicks a node that is a link, it opens up that url in the current browser window. If the node has children I cannot see them, because when I click on the + icon, it also performs that same action and takes me to that URL instead of expanding the node.

Below is my code.

   private final TreeSelectionListener tsl = new TreeSelectionListener()   {      public void valueChanged( TreeSelectionEvent e )      {         TreePath path = e.getPath();         DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) path.getLastPathComponent();         myNode eNode = ( myNode ) node.getUserObject();         if ( eNode.isLink() )         {            showStatus( "Opening page : " + eNode.toString() );            getAppletContext().showDocument( eNode.getURL(), "_top" );         }      }   }; ...   tree.getSelectionModel().addTreeSelectionListener( tsl );...
 

Posts:254
Registered: 4/15/04
Re: Different action performed for JTree  
Jun 21, 2004 7:36 AM (reply 1 of 2)



 
Anyone have any ideas?
 

Posts:254
Registered: 4/15/04
Re: Different action performed for JTree  
Jun 22, 2004 6:16 AM (reply 2 of 2)



 
OK, I think I got it working. I will post the code for anyone who may need it. Note, this is a quick and dirty version that has irrelevant code stripped out.

import javax.swing.*;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeCellRenderer;import javax.swing.tree.TreePath;import java.awt.*;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionAdapter; /** * Renders the cells of the JTree. */final class MyTreeRenderer extends DefaultTreeCellRenderer{   private final static Color BLUE = Color.decode( "#0000aa" );   private final static Color RED = Color.decode( "#FF0000" );    private final static Cursor cursorHand = new Cursor( Cursor.HAND_CURSOR );   private final static Cursor cursorDefault = new Cursor( Cursor.DEFAULT_CURSOR );    public final JTree tree;   private Object lastNode;    private boolean isActiveLink = false;      /**    * Allows another class to check if the mouse is over a hyperlink.    * @return TRUE if over a hyperlink. FALSE if not over a hyperlink.    */    public boolean isActiveLink()   {      return isActiveLink;   }      /**    * Sets whether the mouse is over a hyperlink or not.     * @param b TRUE if the mouse is over a hyperlink, FALSE if the mouse is not over a hyperlink.    */    public void setActiveLink( boolean b)   {      isActiveLink = b;   }    /**    * Contructor.    * @param pTree A reference to the tree.    */   public MyTreeRenderer( JTree pTree )   {      this.tree = pTree;       tree.addMouseMotionListener( new MouseMotionAdapter()      {         public void mouseMoved( MouseEvent me )         {            TreePath treePath = tree.getPathForLocation( me.getX(), me.getY() );            Object obj = ( treePath != null ) ? treePath.getLastPathComponent() : nullif (  obj != null &&  obj != lastNode )  // If mouse is over a node            {               Object userObject = ( ( DefaultMutableTreeNode ) obj ).getUserObject();               MyNode myNode = ( MyNode ) userObject;               if ( myNode.isLink() )                // If node is a hyperlink               {                  tree.setCursor( cursorHand );                  setActiveLink(true);               }               else               {                  tree.setCursor( cursorDefault );  // If node is not a hyperlink                  setActiveLink(false);               }               lastNode = obj;               tree.repaint();            }            else if ( obj == null && obj != lastNode  )  // If mouse is over +/- icon, or no node            {               lastNode = obj;               setActiveLink(false);               tree.setCursor( cursorDefault );               tree.repaint();            }         }      } );   }    public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus )   {     ...etc   }} //...This is how the above class is calledfinal class DynamicTree extends JPanel{   private final JTree tree;    public boolean isActiveLink()   {      MyTreeRenderer etcr =(MyTreeRenderer)tree.getCellRenderer();      return etcr.isActiveLink();   }   public DynamicTree(TreeSelectionListener myTSL)   {     tree = new JTree();     tree.setCellRenderer( new MyTreeRenderer( tree ) );     tree.getSelectionModel().addTreeSelectionListener( tsl );   }} // And finally, everyones favorite...the Appletpublic final class RunTreeAsApplet extends JApplet{   private  DynamicTree treePanelOne;    public void init()   {      treePanelOne = new DynamicTree( tsl );      final JScrollPane treeView = new JScrollPane( treePanelOne );      getContentPane().add( treeView, BorderLayout.CENTER );   }    /**    * Selection Listener for tree nodes that are hyperlinks.    */   private final TreeSelectionListener tsl = new TreeSelectionListener()   {      public void valueChanged( TreeSelectionEvent e )      {         TreePath path = e.getPath();         DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) path.getLastPathComponent();         MyNode eNode = ( MyNode ) node.getUserObject();         if ( eNode.isLink() && treePanelOne.isActiveLink() )         {            showStatus( "Opening page : " + eNode.toString() );            getAppletContext().showDocument( eNode.getURL(), "_top" );         }      }   };}  
 
This topic has 2 replies on 1 page.