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 ::

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTree;
import java.awt.Rectangle;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JPanel;

public class Frame1 extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    JButton jButton1 = new JButton();
    JTree jTree1 = new JTree();
    JSplitPane jSplitPane1 = new JSplitPane();
    JPanel jPanel1 = new JPanel();


    public Frame1() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        setBounds(0,0,500,800);


       // jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
     
        jTree1.setBounds(new Rectangle(4, 31, 106, 129));
        jSplitPane1.setBounds(new Rectangle(17, 168, 179, 25));
        jPanel1.setBounds(new Rectangle(0, 9, 218, 210));
        this.add(new JScrollPane(jTree1), BorderLayout.CENTER);

   //jTree1.expandRow(1);
   jTree1.setScrollsOnExpand(false);
//  this.getContentPane().add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(new JTree()),new JPanel()));  
       // this.getContentPane().add(jScrollPane1);
        this.getContentPane().add(jTree1);
        this.getContentPane().add(jSplitPane1);
        this.getContentPane().add(jPanel1);
        //  this.getContentPane().add(jScrollBar1);
      //  jScrollBar1.setBounds(new Rectangle(227, 102, 17, 129));
          setVisible(true);
    }

    public static void main(String[] args) {
        Frame1 frame1 = new Frame1();
    }

    public void jButton1_actionPerformed(ActionEvent e) {

    }
}

This works for me:

qTree = new JTree(....);

qPane = new JScrollPane(qTree,
      JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

contentPane.add(qPane...);

This works for me:

qTree = new JTree(....);

qPane = new JScrollPane(qTree,
      JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

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, ??

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.

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 ..

Try putting some data in your tree.

I added this to the top of my code

public class Frame1 extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    JButton jButton1 = new JButton();
[B]    String h[]= {"DD","d","DD","d","DD","d","DD","d","DD","d","DD","d","DD","d","F"};
    JTree jTree1 = new JTree(h);[/B]
    JSplitPane jSplitPane1 = new JSplitPane();
    JPanel jPanel1 = new JPanel();
    JScrollPane jScrollPane1 = new JScrollPane();

But still it doesn't scroll .... :(

JScrollPane jScrollPane1 = new JScrollPane();

You haven't read what I posted.

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 .. :(

qPane = new JScrollPane(qTree,..

jScrollPane1 = new JScrollPane();

Spot the difference

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTree;
import java.awt.Rectangle;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JPanel;
import java.awt.Container;

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();
    [B]JScrollPane jScrollPane1 = new JScrollPane();[/B]


    public Frame1() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        setBounds(0,0,500,800);


       // jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));

        jTree1.setBounds(new Rectangle(4, 31, 106, 129));
        jSplitPane1.setBounds(new Rectangle(17, 168, 179, 25));
        jPanel1.setBounds(new Rectangle(0, 6, 218, 210));
        jScrollPane1 = new JScrollPane(jTree1,
      JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        Container content = this.getContentPane();
    [B]    JScrollPane scrollpane_ind = new JScrollPane(jTree1);
  // Make the scrollbars always appear[/B]

[B]scrollpane_ind.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  scrollpane_ind.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);[/B]
  int treewidth = 240;
int treeheight = 700;

//this.addComponent(this, scrollpane_ind, 5,20,treewidth,treeheight);

        this.add(new JScrollPane(jTree1), BorderLayout.CENTER);
        
        JScrollPane scrollPane = new JScrollPane(jTree1);
    content.add(scrollPane, BorderLayout.CENTER);
   //jTree1.expandRow(1);
   jTree1.setScrollsOnExpand(false);
//  this.getContentPane().add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(new JTree()),new JPanel()));
       // this.getContentPane().add(jScrollPane1);
        this.getContentPane().add(jTree1);
        this.getContentPane().add(jSplitPane1);
        this.getContentPane().add(jPanel1);
        jPanel1.add(jScrollPane1);
        //  this.getContentPane().add(jScrollBar1);
      //  jScrollBar1.setBounds(new Rectangle(227, 102, 17, 129));
          setVisible(true);
    }

    public static void main(String[] args) {
        Frame1 frame1 = new Frame1();
    }

    public void jButton1_actionPerformed(ActionEvent e) {

    }
}

See the bolded code segment i have added what ever you told ... :(

That's the first time you showed that pert of the code, I was going on what you had shown.
So, now I see four "new JScrollPane"s and a least three places where you add a scrollpane. I have no idea what the final panel will end up showing!
It's time to go through the code and clean this all up. Just one "new" and one "add" is right.
Please also post code with as the opening tag.[CODE=JAVA] as the opening tag.

This is a cleaned up version of my java Code.... :( i am lost !

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTree;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import java.awt.Container;

public class Frame2 extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    String data []={"G", "gg","G", "gg","G", "gg","G", "gg","G", "gg","G", "gg","G", "gg"};
    JTree jTree1 = new JTree(data);
    JScrollPane jScrollPane1 = new JScrollPane();

    public Frame2() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        setBounds(0,0,500,800);
        
        jScrollPane1 = new JScrollPane(jTree1,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Container content = this.getContentPane();
JScrollPane scrollpane_ind = new JScrollPane(jTree1);

       
        jTree1.setBounds(new Rectangle(19, 25, 124, 163));
        this.getContentPane().add(jTree1);
        this.getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(new Rectangle(133, 170, 2, 2));

         setVisible(true);
    }

    public static void main(String[] args) {
        Frame2 frame2 = new Frame2();
    }
}

You still jave multiple "new"s for scroll panes. You need exactly one.
And don't add the tree to your panel, jusr add it to the scroller and ad the scroller to the panel.
I'll be back in a few hours.

You still jave multiple "new"s for scroll panes. You need exactly one.
And don't add the tree to your panel, jusr add it to the scroller and ad the scroller to the panel.
I'll be back in a few hours.

Ok .. i badly need help !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.