Results 1 to 7 of 7
  1. #1
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default Adding Multiple Panels and Single Scroll bar on the JFrame

    I need to add multiple panels on the Jframe one after another(Vertically). To visualize the Problem let us take an example. In the website, there are multiple panels on the Frame and in the right most Part of Frame, it has a Scrollbar to view items present at the bottom. But I am confused how I can do this.

    JPanel p=new JPanel();
    p.setLayout(null);
    JPanel p1=new JPanel();
    p1.setLayout(null);
    JPanel p2=new JPanel();
    p2.setLayout(null);
    JPanel p3=new JPanel();
    p3.setLayout(null);


    these panels contains different components. and finally added to JFrame which has null Layout.

    But dont know which parameter I should pass in JScrollPane
    JScrollPane js=new JScrollPane(???);

    so that my purpose get solved.

    Secondly How I can add ScrollBar to any Panel in the above mentioned.It means two scroll bar on the same Frame;

    Best Example to visualize the problem is : See the WebPage that appears on pressing Post New Thread.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class MultiPanels {
        private JScrollPane getContent() {
            Dimension d = new Dimension(300,200);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc= new GridBagConstraints();
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(getPanel(d,  6, Color.red), gbc);
            panel.add(getPanel(d,  4, Color.green.darker()), gbc);
            panel.add(getPanel(d, 12, Color.blue), gbc);
            panel.add(getEmptyPanel(d), gbc);
            return new JScrollPane(panel);
        }
    
        private JScrollPane getPanel(Dimension d, int rows, Color color) {
            JPanel panel = new JPanel(new GridBagLayout());
            panel.setBackground(color);
            GridBagConstraints gbc= new GridBagConstraints();
            gbc.insets = new Insets(10,5,10,5);
            gbc.weightx = 1.0;
            for(int i = 0, j = 1; i < rows; i++) {
                gbc.gridwidth = GridBagConstraints.RELATIVE;
                panel.add(new JButton(String.valueOf(j++)), gbc);
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                panel.add(new JButton(String.valueOf(j++)), gbc);
            }
            JScrollPane scrollPane = new JScrollPane(panel);
            scrollPane.setPreferredSize(d);
            return scrollPane;
        }
    
        private JScrollPane getEmptyPanel(Dimension d) {
            JPanel panel = new JPanel() {
                protected void paintComponent(Graphics g) {
                    int w = getWidth();
                    int h = getHeight();
                    GradientPaint gp = new GradientPaint(0,0,Color.red,
                                                         0,h,Color.cyan);
                    ((Graphics2D)g).setPaint(gp);
                    g.fillRect(0,0,w,h);
                }
            };
            // Default size for a JPanel is (10,10). Its layout
            // manager computes its preferredSize while laying
            // out the child components. If there are no children
            // the panel reports its default size to its parent.
            // Use the [i]setPreferredSize[/i] method to provide
            // a size hint to the parent of this component.
            panel.setPreferredSize(new Dimension(300,400));
            JScrollPane scrollPane = new JScrollPane(panel);
            scrollPane.setPreferredSize(d);
            return scrollPane;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new MultiPanels().getContent());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

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

    public class MultiPanels {
    JButton b1;
    private JScrollPane getContent() {
    Dimension d = new Dimension(300,200);
    JPanel panel = new JPanel(null);
    // GridBagConstraints gbc= new GridBagConstraints();
    // gbc.weightx = 1.0;
    // gbc.fill = GridBagConstraints.HORIZONTAL;
    // gbc.gridwidth = GridBagConstraints.REMAINDER;
    panel.add(getPanel(d, 6, Color.red));
    panel.add(getPanel(d, 4, Color.green.brighter()));
    panel.add(getPanel(d, 12, Color.blue));
    // panel.add(getEmptyPanel(d), gbc);
    return new JScrollPane(panel);
    }

    private JScrollPane getPanel(Dimension d, int rows, Color color) {
    JPanel panel = new JPanel(null);
    panel.setBackground(color);

    b1=new JButton("ok");
    b1.setLocation(100, 100);
    b1.setSize(70,20);
    panel.add(b1);

    b1=new JButton("ok");
    b1.setLocation(100, 300);
    b1.setSize(70,20);
    panel.add(b1);

    b1=new JButton("ok");
    b1.setLocation(100, 500);
    b1.setSize(70,20);
    panel.add(b1);

    b1=new JButton("ok");
    b1.setLocation(100, 700);
    b1.setSize(70,20);
    panel.add(b1);
    // GridBagConstraints gbc= new GridBagConstraints();
    // gbc.insets = new Insets(10,5,10,5);
    // gbc.weightx = 1.0;
    // for(int i = 0, j = 1; i < rows; i++) {

    // gbc.gridwidth = GridBagConstraints.RELATIVE;
    // panel.add(new JButton(String.valueOf(j++)), gbc);
    // gbc.gridwidth = GridBagConstraints.REMAINDER;
    // panel.add(new JButton(String.valueOf(j++)), gbc);
    // }
    JScrollPane scrollPane = new JScrollPane(panel);
    scrollPane.setPreferredSize(d);
    return scrollPane;
    }

    /*private JScrollPane getEmptyPanel(Dimension d) {
    JPanel panel = new JPanel() {
    protected void paintComponent(Graphics g) {
    int w = getWidth();
    int h = getHeight();
    GradientPaint gp = new GradientPaint(0,0,Color.red,
    0,h,Color.cyan);
    ((Graphics2D)g).setPaint(gp);
    g.fillRect(0,0,w,h);
    }
    };
    // Default size for a JPanel is (10,10). Its layout
    // manager computes its preferredSize while laying
    // out the child components. If there are no children
    // the panel reports its default size to its parent.
    // Use the setPreferredSize method to provide
    // a size hint to the parent of this component.
    panel.setPreferredSize(new Dimension(300,400));
    JScrollPane scrollPane = new JScrollPane(panel);
    scrollPane.setPreferredSize(d);
    return scrollPane;
    }*/

    public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new MultiPanels().getContent());
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }
    }

  4. #4
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    Thanks for this code. But I Have a problem in NULL Layout.

    I made following changes in your code. Their is no error in this code but a blank screen is displayed.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
            b1=new JButton("ok");
            b1.setLocation(100, 700);
            b1.setSize(70,20);
            panel.add(b1);
            // The scrollPane will show this panel at its
            // preferredSize which is:
            Dimension dim = panel.getPreferredSize();
            System.out.printf("panel preferredSize = [%d, %d]%n",
                               dim.width, dim.height);
            JScrollPane scrollPane = new JScrollPane(panel);
            scrollPane.setPreferredSize(d);
            return scrollPane;
        }

  6. #6
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    Still the problem is not solved. Blank screen is Displayed. The output of S.o.p is
    panel preferredSize = [0, 0]
    panel preferredSize = [0, 0]
    panel preferredSize = [0, 0]

    Can you tell me the solution????????
    even I pass [0,0] as parameter but the o/p is same

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

Similar Threads

  1. Replies: 7
    Last Post: 07-09-2008, 12:06 AM
  2. Adding and removing panels dynamically
    By kbyrne in forum AWT / Swing
    Replies: 1
    Last Post: 04-12-2008, 08:28 PM
  3. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  4. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 04:22 PM
  5. Jtextarea and scroll
    By ziniestro in forum AWT / Swing
    Replies: 2
    Last Post: 06-01-2007, 03:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •