Add a Scroller to JTree <Beginner Question>

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Add a Scroller to JTree <Beginner Question>

 
0
  #1
Sep 27th, 2009
i need to a Scroll bar or JScrollPane to My Jtree... I have googled and gone through several examples, but non seemed to have worked...

Please can some one help me to solve this problem .. I simply need to have scrollers when the content of the JTree increases ..

My Code ::

  1.  
  2.  
  3.  
  4. import java.awt.BorderLayout;
  5. import javax.swing.JFrame;
  6. import javax.swing.JButton;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.JTree;
  10. import java.awt.Rectangle;
  11. import javax.swing.JScrollBar;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JSplitPane;
  14. import javax.swing.JPanel;
  15.  
  16. public class Frame1 extends JFrame {
  17. BorderLayout borderLayout1 = new BorderLayout();
  18. JButton jButton1 = new JButton();
  19. JTree jTree1 = new JTree();
  20. JSplitPane jSplitPane1 = new JSplitPane();
  21. JPanel jPanel1 = new JPanel();
  22.  
  23.  
  24. public Frame1() {
  25. try {
  26. jbInit();
  27. } catch (Exception exception) {
  28. exception.printStackTrace();
  29. }
  30. }
  31.  
  32. private void jbInit() throws Exception {
  33. getContentPane().setLayout(null);
  34. setBounds(0,0,500,800);
  35.  
  36.  
  37. // jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
  38.  
  39. jTree1.setBounds(new Rectangle(4, 31, 106, 129));
  40. jSplitPane1.setBounds(new Rectangle(17, 168, 179, 25));
  41. jPanel1.setBounds(new Rectangle(0, 9, 218, 210));
  42. this.add(new JScrollPane(jTree1), BorderLayout.CENTER);
  43.  
  44. //jTree1.expandRow(1);
  45. jTree1.setScrollsOnExpand(false);
  46. // this.getContentPane().add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(new JTree()),new JPanel()));
  47. // this.getContentPane().add(jScrollPane1);
  48. this.getContentPane().add(jTree1);
  49. this.getContentPane().add(jSplitPane1);
  50. this.getContentPane().add(jPanel1);
  51. // this.getContentPane().add(jScrollBar1);
  52. // jScrollBar1.setBounds(new Rectangle(227, 102, 17, 129));
  53. setVisible(true);
  54. }
  55.  
  56. public static void main(String[] args) {
  57. Frame1 frame1 = new Frame1();
  58. }
  59.  
  60. public void jButton1_actionPerformed(ActionEvent e) {
  61.  
  62. }
  63. }
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 996
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #2
Sep 27th, 2009
This works for me:

  1. qTree = new JTree(....);
  2.  
  3. qPane = new JScrollPane(qTree,
  4. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  5. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  6.  
  7. contentPane.add(qPane...);
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #3
Sep 27th, 2009
Originally Posted by JamesCherrill View Post
This works for me:

  1. qTree = new JTree(....);
  2.  
  3. qPane = new JScrollPane(qTree,
  4. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  5. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  6.  
  7. contentPane.add(qPane...);

But It doesn't work in my application. i am using JBuilder as my IDE. could you help me add the code to my application, ??
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 996
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #4
Sep 27th, 2009
I'm not going to code your app for you, but my code was just showing you the three simple steps to make your tree scrollable, viz:
create the tree
create a scroll pane usinhg the tree as its parameter
add the scroll pane to your frame

ps: "It doesn't work in my application" tells me nothing useful.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #5
Sep 27th, 2009
I have created the tree, and the scrol Panel as seen in my code.and then added the scrolPane to my Frame .....

But theres no output as such. could some one help ..
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 996
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #6
Sep 27th, 2009
Try putting some data in your tree.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #7
Sep 27th, 2009
I added this to the top of my code
public class Frame1 extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    JButton jButton1 = new JButton();
    String h[]= {"DD","d","DD","d","DD","d","DD","d","DD","d","DD","d","DD","d","F"};
    JTree jTree1 = new JTree(h);
    JSplitPane jSplitPane1 = new JSplitPane();
    JPanel jPanel1 = new JPanel();
    JScrollPane jScrollPane1 = new JScrollPane();

But still it doesn't scroll ....
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 996
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #8
Sep 27th, 2009
JScrollPane jScrollPane1 = new JScrollPane();
You haven't read what I posted.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #9
Sep 27th, 2009
Originally Posted by JamesCherrill View Post
You haven't read what I posted.
i did.. if you my code above, i have followed each and every step of what you have said ..
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 996
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: Add a Scroller to JTree <Beginner Question>

 
0
  #10
Sep 27th, 2009
qPane = new JScrollPane(qTree,..
jScrollPane1 = new JScrollPane();
Spot the difference
Reply With Quote Quick reply to this message  
Reply

Tags
jtree, scrollbar

Message:


Thread Tools Search this Thread



Tag cloud for jtree, scrollbar
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC