JScrollPane does not appear when using it on JPanel

For many hours I tried to find a way to solve the problem, but I was not lucky. Here is a sample code:

import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class Example extends JFrame
{
    private static final long serialVersionUID = 1L;

    public Example()
    {
        JPanel contentPane = (JPanel) getContentPane();
        contentPane.setLayout(new BorderLayout());

        JPanel panTop = new JPanel(new BorderLayout());
        //JPanel panBottom = new JPanel(new BorderLayout());

        JPanel panTopCenter = new JPanel();
        //JPanel panTopLeft = new JPanel();
        //JPanel panTopRight = new JPanel();

        panTop.add(panTopCenter, BorderLayout.CENTER);
        //panTop.add(panTopLeft, BorderLayout.WEST);
        //panTop.add(panTopRight, BorderLayout.EAST);

        contentPane.add(panTop, BorderLayout.CENTER);
        //contentPane.add(panBottom, BorderLayout.SOUTH);

        JPanel pan = new JPanel();
        pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
        for(int i = 0; i < 50; i++) pan.add(new JButton("Button " + i));
        JScrollPane scrollPane = new JScrollPane(pan);
        panTopCenter.add(scrollPane);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {           
            @Override
            public void run()
            {
                new Example();
            }
        });
    }
}

Snapshot:

enter image description here

+5
source share
4 answers

I always need to set the size of the preferred size in the view.

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

public class Example extends JFrame {

    public Example() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Box box = new Box(BoxLayout.Y_AXIS);
        for (int i = 0; i < 50; i++) {
            box.add(new JButton("Button " + i));
        }
        JScrollPane sp = new JScrollPane(box);
        Dimension d = new Dimension(box.getComponent(0).getPreferredSize());
        sp.getVerticalScrollBar().setUnitIncrement(d.height);
        d.height *= 10; // Show at least 10 buttons
        sp.getViewport().setPreferredSize(d);

        add(sp, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Example e = new Example();
            }
        });
    }
}
+7
source

Do not set dimensions! If this change is made, a scroll bar appears.

JPanel panTopCenter = new JPanel(new GridLayout());

, FlowLayout , , , ( ) 0x0. GridLayout , .

+7

, , JScrollPane JComponent .

scrollPane.setPreferredSize(new Dimension(100,500));

GridLayout JComponent s.

+3

, , JXPanel SwingX, Scrollable.

0
source

All Articles