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?
Code :
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
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
Re: Need help with my JTree add JScrollPane
What is happening (or not happening) that shouldn't (or should)?
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.
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.
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
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.
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
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:
Code java:
//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.
Re: Need help with my JTree add JScrollPane
Oki I'm going to try it. Let's hope that it will work
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?
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.
Code java:
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.
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.
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. :-bd