Results 1 to 12 of 12
  1. #1
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Question add MouseListener to a JTree

    Hello there

    I have found a MouseListener in the Internet.

    I have the following node:

    Java Code:
    DefaultMutableTreeNode inbox = new DefaultMutableTreeNode("Inbox");
    and the following MouseListener:

    Java Code:
            tree.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent me) {
                doMouseClicked(me);
              }
            });
    and here the doMouseClicked(me);

    Java Code:
          void doMouseClicked(MouseEvent me) {
            TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
            if (){}
            else{}
    }
    The tree is embedded in a left splitpane.
    When I click on the inbox node I want it to show the table of the inbox class in the splitpane to the right.
    Can anyone help me please?

    Thanks,
    Roobiks

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: add MouseListener to a JTree

    I knew I left it somewhere. Thanks for finding it, I really should take more care putting it somewhere safe.

    From the TreePath you get which node was clicked. Your application should have a way to switch content in the right pane. The best choice would probably be a CardLayout. Dividing your application up in sections has nothing to do with trees and listeners. A click on a treenode is just one way of working with the sections.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: add MouseListener to a JTree

    Quote Originally Posted by SurfMan View Post
    I knew I left it somewhere. Thanks for finding it, I really should take more care putting it somewhere safe.

    From the TreePath you get which node was clicked. Your application should have a way to switch content in the right pane. The best choice would probably be a CardLayout. Dividing your application up in sections has nothing to do with trees and listeners. A click on a treenode is just one way of working with the sections.
    Okay.
    How can I switch the content into the right pane when I click on the inbox node?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: add MouseListener to a JTree

    Quote Originally Posted by Roobiks View Post
    Okay.
    How can I switch the content into the right pane when I click on the inbox node?
    You should first come up with a mechanism to work a left/right panel combination. Like I said, using a CardLayout sounds like your best option.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: add MouseListener to a JTree

    Hello

    I have a different approach.
    I have found the following link:
    https://docs.oracle.com/javase/tutor...ents/tree.html

    The functionality is what I would like to use.
    Where in the code does it reference the content to the bottom component? (In my case I use Java classes and not .html content)

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: add MouseListener to a JTree

    Looks like it is in the displayURL method. It displays it in the htmlPane defined as an instance field.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: add MouseListener to a JTree

    hello

    Let's say I want wo differentiate between an "Inbox" node and an "Outbox" node.
    How do I get the name of the node?

    Regard,
    Roobiks

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: add MouseListener to a JTree

    Quote Originally Posted by Roobiks View Post
    hello

    Let's say I want wo differentiate between an "Inbox" node and an "Outbox" node.
    How do I get the name of the node?

    Regard,
    Roobiks
    - JTree.getPathForLocation returns a TreePath
    - TreePath.getLastComponent returns the last Node, which in your case will be a DefaultMutableTreeNode
    - DefaultMutableTreeNode.getUserObject returns an Object that you specified in the constructor, in your case a String which corresponds to the name, but it could be any arbitrary object.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: add MouseListener to a JTree

    I hope someone can help me on this.

    I have the following mainclass (I removed the unnecessary parts in order to maintain readability):

    Java Code:
    import java.awt.BorderLayout;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    import javax.swing.ImageIcon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class mainclass extends JPanel {
    
      private static void createAndShowGUI() {
    
    	    JFrame frame = new JFrame("Test Frame");	    
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    frame.add(center_windows.createCenterPanel(), BorderLayout.CENTER);
    	    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    	    frame.pack();
    	    frame.setVisible(true);
    
      public static void main(String s[]) {
    
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI();
              }
          });
      }
      }
    The content of center_windows:

    Java Code:
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    
    public class center_windows extends JPanel{
    
    	public static JSplitPane splitPane;
    	public static Component createCenterPanel() {
    
            JPanel left = new Tree();
            JPanel right = new Table_intro();
         
            JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 
            splitpane.setLeftComponent(left);
            splitpane.setRightComponent(right);
            splitpane.setDividerLocation(200); 
            JScrollPane scrollPane = new JScrollPane(left);
            JScrollPane scrollPane2 = new JScrollPane(right);
            splitpane.add(scrollPane);
            splitpane.add(scrollPane2);
    	return splitpane;
    
    	}
    }
    So on the left I have a JTree and on the right some content.
    In JTree I am able to differentiate between the nodes.
    This is the TreeSelectionListener of Tree().

    Java Code:
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
              DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
              String string = node.toString();
              
              if (string.equals("Inbox")){
    
    // Here I want to add code      	  
              }                    
            }
          });
    Which code lines do I have to add in the if statement in order to show the content of a class (in this case in contains a textarea) in the right component, when I click on the node?

    Hopefully my post is understandable,
    Roobiks

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: add MouseListener to a JTree

    You keep asking the same question, but nothing changes. Your tree needs access to the right panel to change components there. Nevermind, let me create an example. Hold on....
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: add MouseListener to a JTree

    So here it is. This code lacks some serious optimisations, like String constants for nodes, proper UserObjects in the nodes, it breaks on case sensitivity, but it demonstrates how you control the right panel.

    Java Code:
    import javax.swing.*;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreeNode;
    import java.awt.*;
    
    public class PanelTree extends JComponent {
        //we keep a reference to the CardLayout since we need to use it to switch cards.
        private final CardLayout cardLayout;
        private JPanel rightPanel;
    
        //constants for the cardlayout
        public static final String INBOX = "inbox";
        public static final String SENT_ITEMS= "sent_items";
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame("Test Frame");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new PanelTree());
                    frame.pack();
                    frame.setLocationRelativeTo(null); //this centers the window on screen
                    frame.setVisible(true);
    
                }
            });
        }
    
        public PanelTree() {
            setLayout(new BorderLayout());
    
            TreeNode root = createTreeNodes();
            JTree tree = new JTree(root);
            tree.addTreeSelectionListener(new TreeSelectionListener() {
                @Override
                public void valueChanged(TreeSelectionEvent e) {
                    DefaultMutableTreeNode clicked = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                    String node = clicked.getUserObject().toString();
                    switch (node) {
                        case "Inbox":
                            //switch to the correct card.
                            cardLayout.show(rightPanel, INBOX);
                            break;
                        case "Sent items":
                            cardLayout.show(rightPanel, SENT_ITEMS);
                            break;
                    }
                }
            });
    
    
            cardLayout = new CardLayout();
            rightPanel = new JPanel(cardLayout);
            rightPanel.add(new InboxPanel(), INBOX);
            rightPanel.add(new SentItemsPanel(), SENT_ITEMS);
            //Inbox will be selected by default
            cardLayout.show(rightPanel, INBOX);
    
            JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(tree), rightPanel);
    
            this.add(split, BorderLayout.CENTER);
    
        }
    
        private TreeNode createTreeNodes() {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Mail");
            DefaultMutableTreeNode inbox = new DefaultMutableTreeNode("Inbox");
            DefaultMutableTreeNode sentItems = new DefaultMutableTreeNode("Sent items");
    
            root.add(inbox);
            root.add(sentItems);
    
            return root;
        }
    
    
        private class InboxPanel extends JPanel {
            public InboxPanel() {
                add(new JLabel("Inbox"));
            }
        }
    
        private class SentItemsPanel extends JPanel {
            public SentItemsPanel() {
                add(new JLabel("Sent items"));
            }
        }
    
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: add MouseListener to a JTree

    Quote Originally Posted by SurfMan View Post
    So here it is. This code lacks some serious optimisations, like String constants for nodes, proper UserObjects in the nodes, it breaks on case sensitivity, but it demonstrates how you control the right panel.

    Java Code:
    import javax.swing.*;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreeNode;
    import java.awt.*;
    
    public class PanelTree extends JComponent {
        //we keep a reference to the CardLayout since we need to use it to switch cards.
        private final CardLayout cardLayout;
        private JPanel rightPanel;
    
        //constants for the cardlayout
        public static final String INBOX = "inbox";
        public static final String SENT_ITEMS= "sent_items";
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame("Test Frame");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new PanelTree());
                    frame.pack();
                    frame.setLocationRelativeTo(null); //this centers the window on screen
                    frame.setVisible(true);
    
                }
            });
        }
    
        public PanelTree() {
            setLayout(new BorderLayout());
    
            TreeNode root = createTreeNodes();
            JTree tree = new JTree(root);
            tree.addTreeSelectionListener(new TreeSelectionListener() {
                @Override
                public void valueChanged(TreeSelectionEvent e) {
                    DefaultMutableTreeNode clicked = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                    String node = clicked.getUserObject().toString();
                    switch (node) {
                        case "Inbox":
                            //switch to the correct card.
                            cardLayout.show(rightPanel, INBOX);
                            break;
                        case "Sent items":
                            cardLayout.show(rightPanel, SENT_ITEMS);
                            break;
                    }
                }
            });
    
    
            cardLayout = new CardLayout();
            rightPanel = new JPanel(cardLayout);
            rightPanel.add(new InboxPanel(), INBOX);
            rightPanel.add(new SentItemsPanel(), SENT_ITEMS);
            //Inbox will be selected by default
            cardLayout.show(rightPanel, INBOX);
    
            JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(tree), rightPanel);
    
            this.add(split, BorderLayout.CENTER);
    
        }
    
        private TreeNode createTreeNodes() {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Mail");
            DefaultMutableTreeNode inbox = new DefaultMutableTreeNode("Inbox");
            DefaultMutableTreeNode sentItems = new DefaultMutableTreeNode("Sent items");
    
            root.add(inbox);
            root.add(sentItems);
    
            return root;
        }
    
    
        private class InboxPanel extends JPanel {
            public InboxPanel() {
                add(new JLabel("Inbox"));
            }
        }
    
        private class SentItemsPanel extends JPanel {
            public SentItemsPanel() {
                add(new JLabel("Sent items"));
            }
        }
    
    }
    Hey SurfMan thank you for your reply.
    I will try to add this to my code.
    I simply couldn't figure it out by myself.
    I am glad you are helping me,
    Roobiks
    Last edited by Roobiks; 05-11-2016 at 11:55 AM.

Similar Threads

  1. MouseListener for everything?
    By DFTBA in forum New To Java
    Replies: 2
    Last Post: 07-22-2012, 01:07 PM
  2. Help with mouselistener
    By kastraki2000 in forum Java Applets
    Replies: 2
    Last Post: 04-30-2011, 03:56 PM
  3. can't get x and y from mouselistener
    By j2me64 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2010, 04:57 PM
  4. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  5. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 08:10 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
  •