Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JScrollPane does not appear when using it on a JPanel

I have been trying for hours to find a way to solve the issue, but I had no luck with that. 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

like image 825
Eng.Fouad Avatar asked Jun 13 '12 07:06

Eng.Fouad


People also ask

What is jscrollpane in Java?

Overview of JScrollPane in Java JScrollPane is used to give a scrollable view to your component. When the screen size is small or limited, we can use a scroll pane to showcase a large component or a component whose size changes dynamically.

What is the use of J scroll pane in jQuery?

JScrollPane ( ) Creates an empty scroll pane (no viewPort). It can have both vertical and horizontal scrollbars when needed. 2. JscrollPane (Component c) Creates a scroll pane with the specified component. When the component content is larger than the view, then horizontal and vertical scrollbar appears.

What is the use of a scroll pane in react?

JScrollPane is used to give a scrollable view to your component. When the screen size is small or limited, we can use a scroll pane to showcase a large component or a component whose size changes dynamically.


2 Answers

I always have to set the viewport's preferred size like this.

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();
            }
        });
    }
}
like image 121
Catalina Island Avatar answered Sep 26 '22 16:09

Catalina Island


Don't set any sizes! The scroll-bar appears if this change is made.

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

The basic problem is that FlowLayout will show components at the smallest size needed to display it, and for a scroll-pane, that is (decided to be) 0x0. By using a GridLayout with no arguments in the constructor and adding the scroll-pane as the only component, it will be forced to fill the available space.

like image 24
Andrew Thompson Avatar answered Sep 22 '22 16:09

Andrew Thompson