javaswingjframejpaneljscrollpane

JScrollPane not working with JPanel and JFrame


For my code, I did something like:

Jframe frame = new JFrame();
JPanel panel = new JPanel();

JButton button = new JButton();
// after locating and resizing buttons...
panel.add(button);

// and after many codes about buttons and labels...

frame.add(panel);

JScrollPane scroll = new JScrollPane(panel);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scroll);

As far as I know, this should be correct, but I can't scroll. The gray bar inside the scroll bar is missing.

Here is image for better idea of what's happening to my code.


Solution

  • If you read the official tutorial about the scroll panes on https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html, they note about the policies:

    VERTICAL_SCROLLBAR_ALWAYS

    HORIZONTAL_SCROLLBAR_ALWAYS

    Always display the scroll bar. The knob disappears if the viewport is large enough to show the whole client.