943,865 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1680
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 27th, 2009
0

Add a Scroller to JTree <Beginner Question>

Expand Post »
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 ::

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

This works for me:

JAVA Syntax (Toggle Plain Text)
  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...);
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

This works for me:

JAVA Syntax (Toggle Plain Text)
  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, ??
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

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.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

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 ..
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

Try putting some data in your tree.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

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 ....
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

Quote ...
JScrollPane jScrollPane1 = new JScrollPane();
You haven't read what I posted.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

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 ..
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Sep 27th, 2009
0

Re: Add a Scroller to JTree <Beginner Question>

Quote ...
qPane = new JScrollPane(qTree,..
Quote ...
jScrollPane1 = new JScrollPane();
Spot the difference
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Hibernate
Next Thread in Java Forum Timeline: star pattern





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC