yogesh meghnani
yogesh meghnani

Reputation: 739

Not able to scroll my JPanel which contains several other panels using JScrollPane

I am having one main JPanel in which I had used several other panels. I want to add a scroll pane to my panel so that when child panel go out of frame they should be scrollable, but I am unable to achieve this.

How to achieve correct scrolling functionality for panels within a panel?

Code inside my child panel:

public class Page13111SubPanel extends JPanel {

    /**
     * Create the panel.
     */
    public Page13111SubPanel() {
//      setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        setSize(1000,130);
        setLayout(new GridLayout(3, 3, 40, 35));

        JLabel label= new JLabel();
        label.setText("Vehicle Companies:asdddddddddddddddd1");
        label.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label);

        JLabel label1= new JLabel();
        label1.setText("Vehicle Companies:asdddddddddddddddd2");
        label1.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label1);

        JLabel label2= new JLabel();
        label2.setText("Vehicle Companies:asdddddddddddddddd3");
        label2.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label2);

        JLabel label3= new JLabel();
        label3.setText("Vehicle Companies:asdddddddddddddddd4");
        label3.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label3);

//      JLabel label3= new JLabel();
//      label3.setText("Vehicle Companies:asdddddddddddddddd4");
//      label3.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
//      add(label3);

        JLabel label4= new JLabel();
        label4.setText("Vehicle Companies:asdddddddddddddddd5");
        label4.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label4);

        JLabel label5= new JLabel();
        label5.setText("Vehicle Companies:asdddddddddddddddd6");
        label5.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label5);

        JLabel label6= new JLabel();
        label6.setText("Vehicle Companies:asdddddddddddddddd7");
        label6.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label6);
    }

}

Code inside my main panel:

public class Page1311Test extends JPanel {
//  private JTable table;
    public JScrollPane pane=null;
    public JPanel panel=null;
    public JButton back=null;
    /**
     * Create the panel.
     */
    public Page1311Test() {

        JPanel panel=new JPanel();
        panel.setLayout(new GridLayout(7,1,1,40));
        Page13111SubPanel[] panel1=new Page13111SubPanel[7];
            panel1[0]=new Page13111SubPanel();
            panel.add(panel1[0]);
            panel1[1]=new Page13111SubPanel();
            panel.add(panel1[1]);
            panel1[2]=new Page13111SubPanel();
            panel.add(panel1[2]);
            panel1[3]=new Page13111SubPanel();
            panel.add(panel1[3]);
            panel1[4]=new Page13111SubPanel();
            panel.add(panel1[4]);
            panel1[5]=new Page13111SubPanel();
            panel.add(panel1[5]);
            panel1[6]=new Page13111SubPanel();
            panel.add(panel1[6]);
        pane=new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(pane);

    }
public static void main(String[] args) {
    JFrame frame=new JFrame();
    frame.setExtendedState(frame.MAXIMIZED_BOTH);
    frame.setLocation(0, 0);
    frame.add(new Page1311Test());
    frame.setVisible(true);
}
}

Upvotes: 1

Views: 178

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Changing the main panel from FlowLayout (the default) to a layout that uses the child components of the panel to fill the available space (used GridLayout here) solves the problem. E.G.

import java.awt.*;
import javax.swing.*;

public class Page1311Test extends JPanel {

    public JScrollPane pane = null;
    public JPanel panel = null;

    /**
     * Create the panel.
     */
    public Page1311Test() {
        setLayout(new GridLayout());
        //JPanel panel = new JPanel(); //shadowed class attribute
        panel = new JPanel(new GridLayout(0, 1, 1, 40));

        for (int ii = 0; ii < 17; ii++) {
            panel.add(new Page13111SubPanel(ii));
        }

        pane = new JScrollPane(panel,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(pane);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setExtendedState(frame.MAXIMIZED_BOTH);
        frame.setLocation(0, 0);
        frame.add(new Page1311Test());
        frame.pack();
        frame.setVisible(true);
    }
}

class Page13111SubPanel extends JPanel {

    /**
     * Create the panel.
     */
    public Page13111SubPanel(int num) {
        //setSize(1000, 130); // Don't set sizes using 'magic numbers'
        setLayout(new GridLayout(3, 3, 40, 35));

        add(new JLabel(num + " Vehicle Companies:asdddddddddddddddd1"));
        add(new JLabel("Vehicle Make:"));
        add(new JLabel("Vehicle Model"));
        add(new JLabel("Vehicle Number"));
        add(new JLabel("Vehicle Driver:"));
        add(new JLabel("Vehicle Wheels"));
        add(new JLabel("Vehicle Air Con"));
    }
}

Upvotes: 2

matt
matt

Reputation: 12347

I am guessing the problem lies in your GridLayout and setSize calls. When you call, essentially your layout manager is going to overwrite you setSize call.

In the following example, I set the preferred and minimum sizes and the scroll pane works fine.

    JFrame frame = new JFrame("Grids of Grids");

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3, 3));
    for(int i = 0; i<9; i++){
        JPanel localGrid = new JPanel();
        localGrid.setMinimumSize(new Dimension(250, 250));
        localGrid.setPreferredSize(new Dimension(250, 250));

        localGrid.setLayout(new GridLayout(5, 5));
        for(int j = 0; j<25; j++){
            localGrid.add(new JLabel("" + i + ":: " + j));
        }
        panel.add(localGrid);
    }
    JScrollPane scroll = new JScrollPane(panel);
    frame.add(scroll);

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Otherwise, your scrollable area will actuall be pretty small because the layouts will try to fit it into the available space.

Upvotes: 1

S.M.
S.M.

Reputation: 39

I think your problem is that the main Panel is not scrollable, therefore you cannot scroll at all.

Try using a JScrollPane there and take a look at this tutorial to get more information about JScrollPanes. There are also lots of information about other GUI-Objects like Frames and Panels, so I think you can find a solution for your problem!

Upvotes: 0

Related Questions