Hi all,

I was just trying to write a simple prog in which i'm trying to add multiple components to a JPanel and inturn i'm adding that to a JScrollpane. Now the problem i'm facing is when i run the prog i can see the contents of the panel(not all) and i can also see scrollbar but i can't find the knob to move it back and forth please look into my code and help me out.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.*;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneLayout;
import javax.swing.border.LineBorder;

public class AddingToJScrollPane {

    static JScrollPane scroll=null; //new JScrollPane(null);

    public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel=new JPanel();
    panel.setLayout(null);

    JButton jButton1 = new JButton();
    jButton1.setBounds(50, 500, 70, 30);
    panel.add(jButton1);

    int inc=0;
    for(int i=0;i<=100;i++){
    JLabel label = new JLabel("Label");
    label.setBounds(10, 10+inc, 70, 30);
    panel.add(label);
    inc=inc+50;
    } 

    scroll = new JScrollPane (panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setEnabled(true);
    scroll.setPreferredSize(new Dimension(100,100));
    frame.add(scroll);
    scroll.setVisible (true);

    frame.setSize(500, 400);
    frame.setVisible(true);
  }
}

Thanks in advance.

Yet another good example of why not to use a null layout manager!
Swing doesn't know how big the panel is because there's no layout manager to set it. If you use setPreferredSize to give the panel a size bigger than the scroll pane's viewport, the scroll buttons will appear.

commented: thanks a lot ..its working fine.. +0

too hard to say something cleaver (after question is already answered by @JamesCherrill), just about good practicies that haven't any conflict(s) with methods implemeted in Layout Managers APIs (sizing of screen, setXxxSize, ...)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class JPanelsInsideJScrollPane {

    private JFrame frame = new JFrame("JPanels Inside JScrollPane");
    private JScrollPane scrollPane = new JScrollPane();
    private JPanel parentPanel, childOne, childTwo, childThree;
    private JButton button = new JButton("Change, Switch Layout Manager to BoxLayout");

    public JPanelsInsideJScrollPane() {
        parentPanel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(new Dimension(800, 600));
            }

            @Override
            public void paintComponent(Graphics g) {
                int margin = 10;
                Dimension dim = getSize();
                super.paintComponent(g);
                g.setColor(Color.DARK_GRAY);
                g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
            }
        };
        parentPanel.setLayout(new GridLayout(0, 1));
        childOne = new JPanel() {
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(100, 100);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 300);
            }

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(500, 300);
            }

            @Override
            public void paintComponent(Graphics g) {
                int margin = 10;
                Dimension dim = getSize();
                super.paintComponent(g);
                g.setColor(Color.GREEN);
                g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
            }
        };
        childTwo = new JPanel() {
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(100, 100);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(600, 400);
            }

            @Override
            public void paintComponent(Graphics g) {
                int margin = 10;
                Dimension dim = getSize();
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
            }
        };
        childThree = new JPanel() {
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(100, 100);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(800, 600);
            }

            @Override
            public void paintComponent(Graphics g) {
                int margin = 10;
                Dimension dim = getSize();
                super.paintComponent(g);
                g.setColor(Color.ORANGE);
                g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
            }
        };
        parentPanel.add(childOne);
        parentPanel.add(childTwo);
        parentPanel.add(childThree);
        scrollPane.setViewportView(parentPanel);
        scrollPane.getVerticalScrollBar().setUnitIncrement(30);
        scrollPane.getHorizontalScrollBar().setUnitIncrement(30);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LayoutManager manager = parentPanel.getLayout();
                if ((manager != null) && (manager instanceof BoxLayout)) {
                    parentPanel.setLayout(new GridLayout(0, 1));
                    button.setText("Change, Switch Layout Manager to BoxLayout");
                } else if ((manager != null) && (manager instanceof GridLayout)) {
                    parentPanel.setLayout(new BoxLayout(parentPanel, BoxLayout.PAGE_AXIS));
                    button.setText("Change, Switch Layout Manager to GridLayout");
                }
                parentPanel.revalidate();
                parentPanel.repaint();
            }
        });
        frame.add(scrollPane);
        frame.add(button, BorderLayout.SOUTH);
        frame.setSize(400, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JPanelsInsideJScrollPane();
            }
        });
    }
}
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.