Karol Pokomeda
Karol Pokomeda

Reputation: 141

How to make JPanel scrollable?

I'm creating GUI for my university project and I 'm trying to understand how JScrollPane works.

I have successfully written simple program which shows picture in a scrollable way:

public class ScrollPaneTest{
    public static void main(String[] args){
        JFrame testFrame = new JFrame("ramka testowa");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel picture = new JLabel(new ImageIcon("JavaSwingCompoentsList.PNG"));
        JScrollPane scrollPane = new JScrollPane(picture);

        testFrame.add(scrollPane, BorderLayout.CENTER);
        testFrame.setSize(400, 400);
        testFrame.setVisible(true);
    }
}

Although, in my final GUI, I would like to apply JScrollPane only to a part of it, e.g. single JPanel. To test this idea I have written following code, which unfortunately does not work:

public class ScrollPaneTest{
    public static void main(String[] args){
        JFrame testFrame = new JFrame("ramka testowa");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel picture = new JLabel(new ImageIcon("JavaSwingCompoentsList.PNG"));
        JScrollPane scrollPane = new JScrollPane(picture);

        JPanel insidePanel = new JPanel();
        insidePanel.add(scrollPane);
        testFrame.add(insidePanel, BorderLayout.CENTER);

        testFrame.setSize(400, 400);
        testFrame.setVisible(true);

    }
}

I have read numerous tutorials, as well as Stack and CodeRanch articles and I still fail to grasp the idea of how the JScrollPane work. I suspect, that my mistake has something to do with specifying the dimension of JPanel-to-scroll, but every single approach I have tried gave me no scrollbars or no picture at all.

If you could show me the right solution to this problem and most importantly, show me where I was wrong I would be very grateful.

Upvotes: 3

Views: 6772

Answers (4)

Baljeet Verma
Baljeet Verma

Reputation: 1

I just passed my jpanel to the jscrollpane constructor... you can also use vertical and horizontal scroll policies like this...

JPanel sideBar = ......
JScrollPane scrollSideBar = new JScrollPane(sideBar, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168845

The problem seems to come from the default FlowLayout of the inner panel. Change it to another layout (I use BordeLayout) and it should work. Having said that, I cannot explain why flow layout fails!

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ScrollPaneTest {

    public static void main(String[] args) {
        JFrame testFrame = new JFrame("ramka testowa");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel picture = new JLabel(new ImageIcon(
                new BufferedImage(370, 1200, BufferedImage.TYPE_INT_RGB)));
        JScrollPane scrollPane = new JScrollPane(picture);

        JPanel insidePanel = new JPanel(new BorderLayout());
        insidePanel.add(scrollPane);
        testFrame.add(insidePanel, BorderLayout.CENTER);

        insidePanel.add(new JLabel("Stay"), BorderLayout.LINE_START);
        insidePanel.add(new JLabel("Stay"), BorderLayout.LINE_END);
        insidePanel.add(new JLabel("Stay"), BorderLayout.PAGE_START);
        insidePanel.add(new JLabel("Stay"), BorderLayout.PAGE_END);

        testFrame.pack();

        testFrame.setSize(400, 400);
        // failing to do this will end the main & the app.
        // doing it will cause the EDT to start.
        testFrame.setVisible(true);
    }
}

Upvotes: 2

Top Sekret
Top Sekret

Reputation: 758

  1. Initialize JPanel, not JScrollPane with picture
  2. To add the JPanel to JScrollPane, do:

    scrollPane.setViewportView (panel)
    
  3. Add the JScrollPane, not JPanel to the JFrame

Upvotes: 3

FredK
FredK

Reputation: 4084

The simplest way is to create your JPanel and specify it when creating the JScrollPane:

 JPanel myPanel = ...;
 JScrollPane scroller = new JScrollPane( myPanel );

Then just add the scroller to your GUI (instead of adding myPanel ).

Upvotes: 2

Related Questions