Results 1 to 9 of 9
  1. #1
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Lightbulb How to link Jtree TreeNode with JPanels

    Yes I have tried looking everywhere but I don't seem to find any answer to that. I have my assignment pending where I am suppose to implement a GUI. My requirement is to setup a JTree where each node is to be linked to a JPanel. That means whenever a node is clicked the corresponding JPanel is to appear in the SplitPane where the left component is setup as JTree and the right component is set to JPanel. Each of those JPanels has their own classes consisting of JButtons, JLabel and JTestfields. For now I have implemented the JTree with the TreeNodes but I can't link them to JPanels so that when clicked they would appear in as the right component.

    Any help?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to link Jtree TreeNode with JPanels

    Sounds like a job for CardLayout.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to link Jtree TreeNode with JPanels

    Not specific to Eclipse. Moving to AWT/Swing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: How to link Jtree TreeNode with JPanels

    Sorry about that DarrylBurke.

    Anyways so yes i have implemented using the CardLayout but for some unknown reason my TreeSelectionEvent is throwing so many errors.
    I have attached my TreeSelectionEvent code.

    Java Code:
    	public class JTreeSelectionListener implements TreeSelectionListener{
    	 @Override
    		public void valueChanged(TreeSelectionEvent event) {
    	       DefaultMutableTreeNode node = (DefaultMutableTreeNode)
    	                           tree.getLastSelectedPathComponent();
    			 
    
    	        if (node == null) return;
    	         Object nodeinfo = node.getUserObject();
    	         
    	        if (node.isLeaf()) {
    	        if(nodeinfo == "Select Input File"){
    	        		InputFile file = new InputFile();
    	        		layout.show(file, "Input File");	        			        		
    	        	
    	        }
    	        if (DEBUG) {
    	            System.out.println(nodeinfo.toString());
    	        }
    	               	        
    	        }
    	 }
    }

  5. #5
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: How to link Jtree TreeNode with JPanels

    Errors

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at EclipsePackage.GuiFace$JTreeSelectionListener.valueChanged(GuiFace.java:82)
    	at javax.swing.JTree.fireValueChanged(Unknown Source)
    	at javax.swing.JTree$TreeSelectionRedirector.valueChanged(Unknown Source)
    	at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(Unknown Source)
    	at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(Unknown Source)
    	at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(Unknown Source)
    	at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(Unknown Source)
    	at javax.swing.JTree.setSelectionPath(Unknown Source)
    	at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(Unknown Source)
    	at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(Unknown Source)
    	at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to link Jtree TreeNode with JPanels

    If you want help, you'll have to provide an SSCCE (not your whole program, but not an uncompilable snippet) that demonstrates the error you're talking about.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: How to link Jtree TreeNode with JPanels

    Found the solution at least it suits my requirement. I will post back soon.

  8. #8
    Edgar is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Re: How to link Jtree TreeNode with JPanels

    Quote Originally Posted by RepulsiveStyler View Post
    Yes I have tried looking everywhere but I don't seem to find any answer to that. I have my assignment pending where I am suppose to implement a GUI. My requirement is to setup a JTree where each node is to be linked to a JPanel. That means whenever a node is clicked the corresponding JPanel is to appear in the SplitPane where the left component is setup as JTree and the right component is set to JPanel. Each of those JPanels has their own classes consisting of JButtons, JLabel and JTestfields. For now I have implemented the JTree with the TreeNodes but I can't link them to JPanels so that when clicked they would appear in as the right component.

    Any help?

    hey there... i faced the same problem.. but this worked for me

    use the jtree eventlistener of valuechanged.....then do this

    String n=tr.getSelectionPath().getLastPathComponent().toS tring();
    switch (n) {
    case "Home":
    movePane.setVisible(false);
    rptPane.setVisible(false);
    customerPane.setVisible(false);
    reservPane.setVisible(false);
    viewPane.setVisible(true);

    break;
    case "Movements":

    viewPane.setVisible(false);
    rptPane.setVisible(false);
    customerPane.setVisible(false);
    reservPane.setVisible(false);
    movePane.setVisible(true);


    break;
    }

    you can also use if statement

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to link Jtree TreeNode with JPanels

    Edgar, please go through BB Code List - Java Programming Forum

    Also, don't post to old dead threads.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JTree - Implementing TreeNode question
    By ehudfisher in forum AWT / Swing
    Replies: 2
    Last Post: 08-16-2011, 10:23 AM
  2. NullPointerException with treeNode error
    By jasonwucinski in forum New To Java
    Replies: 3
    Last Post: 05-05-2011, 09:27 PM
  3. cast string to treeNode
    By jasonwucinski in forum Advanced Java
    Replies: 1
    Last Post: 04-26-2011, 11:51 PM
  4. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  5. Jtree link to JFrame Form
    By daniel50096230 in forum NetBeans
    Replies: 4
    Last Post: 09-16-2008, 06:53 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
  •