Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Need help with my JTree add JScrollPane

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with my JTree add JScrollPane

    Hello,


    In my program I'm using JTree and I'm having problem with adding JScrollPane. Can somebody please help me?

    mainTab = new JTabbedPane(JTabbedPane.TOP);
    		mainTab.setBackground(Color.LIGHT_GRAY);
    		frame.getContentPane().add(mainTab, BorderLayout.CENTER);
     
    		JPanel panel = new JPanel();
    		mainTab.addTab("New tab", null, panel, null);
    		mainTab.setBackgroundAt(0, Color.WHITE);
    		JScrollPane scrollPane = new JScrollPane(mainTab);
    		frame.getContentPane().add(scrollPane);
    		Container cp = new Container();
    		panel.add(cp);
     
     
    		Schools = new DefaultMutableTreeNode("Schools",true);
    		model = new DefaultTreeModel(Schools);
    		//model.addTreeModelListener(new MyTreeModelListener());
    		EuropeanSchoolLuxembourg = new DefaultMutableTreeNode("School Name",true);
     
    		Schools.add(EuropeanSchoolLuxembourg);
     
    		treeNode2 = new DefaultMutableTreeNode("Building A");
    		EuropeanSchoolLuxembourg.add(treeNode2);
     
    		treeNode3 =new DefaultMutableTreeNode("Room A1");
    		treeNode2.add(treeNode3);
    		treeNode4 = new DefaultMutableTreeNode("Room A2");
    		treeNode2.add(treeNode4);
     
     
    		/*DefaultMutableTreeNode for Uni */
     
    		Uni = new DefaultMutableTreeNode("Uni",true);
     
    		treeNode2 = new DefaultMutableTreeNode("Building A");
    		Uni.add(treeNode2);
     
    		treeNode3 =new DefaultMutableTreeNode("Room A1");
    		treeNode2.add(treeNode3);
    		treeNode4 = new DefaultMutableTreeNode("Room A2");
    		treeNode2.add(treeNode4);
    		Schools.add(Uni);
     
    		/* JTree */
     
    		final JTree tree = new JTree(model);
    		tree.setAutoscrolls(true);
    		tree.setEditable(true);
     
    		tree.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
    		tree.setBorder(UIManager.getBorder("Tree.editorBorder"));
     
    		/*JScrollPane*/
     
    		JScrollPane treePane = new JScrollPane();
    		treePane.setBounds(271, 11, 19, 154);
    		treePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		treePane.setViewportView(tree);
    		cp.add(treePane, BorderLayout.CENTER);

    I think that Im not adding the JScrollPane correctly, can somebody help me? Thank you very much for your Help


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my JTree add JScrollPane

    Oh I posted it here, coz It's in JFrame But maybe I should re-post it to the right directory

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with my JTree add JScrollPane

    What is happening (or not happening) that shouldn't (or should)?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my JTree add JScrollPane

    Oh silly me,

    Well I cant add the JScrollPane to the tree. When I execute my code the JScrollPane isn't in the JTree.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with my JTree add JScrollPane

    Try using the new JScrollPane(tree); constructor instead of the new JScrollPane(); constructor, and tell me if that does anything.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my JTree add JScrollPane

    Nope it doesn't I tried that as well. I don't know what I'm doing wrong.. It just doesn't work..I even tried to create another JPanel instead of the Container but still doesn't work

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with my JTree add JScrollPane

    I suggest trimming your code down to an SSCCE to demonstrate what exactly the issue is, and describing exactly what you need and what the problem is. Unfortunately, the above posts just aren't descriptive enough (at least for me) to help you as the problem (again, at least to me) is not clearly defined.

  8. #8
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my JTree add JScrollPane

    Well,

    I have a JTabbledPane and on it I have several JPanels. On one of those JPanel there is a JTree. Jtree works fine except when I extend all the nodes, the JTree goes far beyond it's borders. And I need the JScrollPane to scroll down to see all the extended nodes.

    But when I try to add the JScrollPane it doesn't show there. And I don't know why is the JScrollPane not working for me. I tried to make another JPanel or Container, but non of this work.

    I'm going to post the whole code so that you can see what is going on: Maybe you can find there why it's not working for sure it's just some small mistake which I can't see.

    Paste2 - Viewing Paste 1996330

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with my JTree add JScrollPane

    Ok. Well the first thing I would do is get rid of all of the extra stuff that may or may not be creating conflicts, since ruling that possibility out is the fastest thing to do right now.

    So, use the constructor I mentioned, and comment out all the extra tree and scroll panel code (and just for now, get rid of the final keyword), so you get this:
    //final JTree tree = new JTree(model);
    JTree tree = new JTree(model);
    //tree.setAutoscrolls(true);
    //tree.setEditable(true);
     
    //tree.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
    //tree.setBorder(UIManager.getBorder("Tree.editorBorder"));
     
    /*JScrollPane*/
     
    JScrollPane treePane = new JScrollPane(tree);
    //treePane.setBounds(271, 11, 19, 154);
    //treePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //treePane.setViewportView(tree);
    cp.add(treePane, BorderLayout.CENTER);

    Now, if that puts the JTree in the JScrollPane, it means that somewhere in the commented out code, a conflict is occurring, and it is just a matter of uncommenting each line one-by-one to find the conflict. So try that, and if it still doesn't add the tree to the scroll pane, we know the new JScrollPane(tree); constructor doesn't solve the problem.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my JTree add JScrollPane

    Oki I'm going to try it. Let's hope that it will work

  11. #11
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my JTree add JScrollPane

    So I did it, here you can see the pic : http://i.imgur.com/g2ero.png

    The ScrollPane is working here, the vertical is it possible to make a horizontal as well?

    So why here it work and in my previous code it doesn't?

  12. #12
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with my JTree add JScrollPane

    Ok, that is a good sign. That confirms that somewhere in the code, there is a conflict (or multiple conflicts). Now we just need to find it. The first thing you should do is swap the final keyword back in, and run it again.

    final JTree tree = new JTree(model);
    //tree.setAutoscrolls(true);
    //tree.setEditable(true);
     
    //tree.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
    //tree.setBorder(UIManager.getBorder("Tree.editorBorder"));
     
    /*JScrollPane*/
     
    JScrollPane treePane = new JScrollPane(tree);
    //treePane.setBounds(271, 11, 19, 154);
    //treePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //treePane.setViewportView(tree);
    cp.add(treePane, BorderLayout.CENTER);

    If it still works, that means that there is no problem using the final keyword. From then on, just uncomment the next line of code and run it again. Keep doing that process until it eventually stops working. Once it stops working, the last line that you uncommented will be the conflict. So comment out that line, and continue with the rest to search for more conflicts. Tell me how that goes.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  13. #13
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my JTree add JScrollPane

    Strange thing just happened , I commented everything out nd made the frame small and JScrollPane appeared? O_o Why? Why I can't see it when the Jtree stretches? In my code I used treePane.setBounds() method.

  14. #14
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my JTree add JScrollPane

    Thank you I just fixed it Rly thx for your help. I was adding it to wrong place.

Similar Threads

  1. How to open files that are represented in a JTree
    By reptar693 in forum AWT / Java Swing
    Replies: 2
    Last Post: March 30th, 2012, 12:11 PM
  2. JTree DnD Problem
    By hafunui in forum AWT / Java Swing
    Replies: 3
    Last Post: August 12th, 2011, 10:33 PM
  3. JTree not expanding?
    By captain alge in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2011, 03:32 AM
  4. JTree - Remove All Nodes
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: December 9th, 2010, 05:27 PM
  5. [SOLVED] Jtree help
    By sman36 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 09:39 AM

Tags for this Thread