tags:

views:

100

answers:

2

I want to add Scrollpane to JPanel which is in turn called by another jpanel?

 public class test {

    JFrame jframe;
    JPanel jpanel;
    JScrollPane js= null;
    public void createFrame()
    {
        jframe=new JFrame("Thick client Wallboard");
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        jframe.setSize(dim.width,dim.height);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setUndecorated(true);
        jframe.setResizable(false);         
        jframe.setLocation(0, 0);
        getJContentPane();
        jframe.setContentPane(jpanel);
        jframe.setVisible(true);
    }
    public void getJContentPane()
    {

        jpanel = new JPanel();
        jpanel.setBackground(new Color(0x505050));
        jpanel.setLayout(null);
        jpanel.setFont(new Font("verdana",Font.BOLD,12));
        jpanel.setBorder(new LineBorder(Color.BLACK,2));
        getpanel();
        jpanel.add(js);
        jpanel.setBackground(Color.LIGHT_GRAY);
        jpanel.setVisible(true);


    }
    public void getpanel()
    {

        JPanel mainPanel=new JPanel();
        mainPanel.setBounds(50,50,920,650);
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));  

        for(int i = 0; i < 30; i++) {  
            mainPanel.add(new JButton("Button " + i));  
        }
        js=new JScrollPane(mainPanel);

    }


    public static void main(String[] args) {

      test t=new test();
      t.createFrame();
    }

}

I have done like this but it is not working...

+3  A: 

JScrollPane is, like all JComponent derivatives, a decorator, and you can decorate any component with it.

You can put any component in the scroll pane.

JScrollPane pane = new JScrollPane(component);

and just add the scrollpane instead of the wrapped component.

Jes
+2  A: 

You can do it:

public static void main(String[] args) {
    Frame frame = new JFrame();  

    JPanel panel = new JPanel();  
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));  

    for(int i = 0; i < 20; i++) {  
      panel.add(new JButton("Button " + i));  
    } 

    //Creating JScrollPane with JPanel
    JScrollPane scrollPane = new JScrollPane(panel);
    JPanel otherPanel = new JPanel();
    otherPanel.setLayout(new BorderLayout());
    //Adding scrollPane to panel
    otherPanel.add(scrollPane);
    frame.add(otherPanel);  

    frame.setSize(200,200);  
    frame.setVisible(true);     

}

Also check : http://www.coderanch.com/t/342486/GUI/java/Adding-ScrollPane-JPanel

YoK
your adding scrollerpane for a jpanel and then the scrollerpane is added to jframe, but i need scrollerpane for a jpanel and then the scrollerpane is added to another jpanel not a frame, please help me on this
Lalchand
So another jpanel is added to jframe ?
YoK
@lalchand Changed code now scrollpane is added to other JPanel.
YoK
your code is working great , but you used layout manager (BorderLayout()) but i need a null layout ,when i changed your code with null layout manager its not working don't know why?
Lalchand