ScrollPanel unable to scroll


Once again i am wasting time building Java GUIs and have hit a problem that I need guidance on.


The structure of my GUI is as follows.

The main frame is JFrame which has BorderLayout and contains a TabbedPanel.
The tabbedPanel contains many JPanels - one for each tab.

These JPanels contain two separate Components using BorderLayout manager (CENTER and LINE_END).

The first component (CENTER) is a JScrollPanel which contains the actual JPanel on which an image is loaded / drawn.

The second component just contains one or two buttons.


Even when the image is much larger than the visible area, the scroll panel doesnt scroll.

My attempts so far

I've followed the examples on the sun site pretty much excalty for the JPanel which hold the image and the JScrollPanel. I feel external forces of other components might be affecting it somehow.

I've tried setting every (size related settings) for both the image panel and the scroll panel but with no result.

Begging my computer.

Running the sun example on my computer is fine so none of my JRE isnt corrupted.


Thanks for your time, my code is available via email should someone be kind enough to take a personl look at it.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dechAuthor Commented:
Update: When I set the scroll bars to always appear, no scrolling is allowed either
try setting the preferred size of your panel either by overriding setPreferredSize() or by calling setPreferrredSize()
If you're panel just paints the image it may be easier to use a JLabel with an ImageIcon instead.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Can't you post your code?
dechAuthor Commented:
I actually want my JPanel to load an PlanarImage (that part of which is working fine), but debugging this thing, I am just drawing large circles to closely match the sun tutorial.

I cant really post my code as it is divided up into a number of files which would be very messy on here. Best if I email it out all together.

> Best if I email it out all together.

aint allowed sorry.

Did you try setting the preferred size of your panel?
dechAuthor Commented:
Yes I did that right at the first.

"I've tried setting every (size related settings) for both the image panel and the scroll panel but with no result."

However, I must inform you that setting the size doesnt seem to affect display sizes. It just seems to stretch out to fill the space available.
>  It just seems to stretch out to fill the space available.

you should be setting the preferred size of the panel, and *not* the scroll pane.
And make you add the scroll pane to your component hierarchy and do not add the panel.
dechAuthor Commented:
Thats what I currently have.
can you just post the code thats creates the tabbed pane, and where you set the preferred size.
dechAuthor Commented:

=============================== (JFrame file)
    public Main(String name) {
        contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        tabbedPane = new TabbedPane(this);
        contentPane.add(tabbedPane, BorderLayout.CENTER);
        buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        // GA Options
        gaButton = new JButton("Set GA Options");
        // Space between the buttons
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        // Exit button
        exitButton = new JButton("Exit");
        contentPane.add(buttonPane, BorderLayout.PAGE_END);

        // Close all other frames if this frame closes

        // Display the window.
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();


      public TabbedPane(Main mainFrame) {
            super(new GridLayout(1, 1));

            tabbedPane = new JTabbedPane();

            originalPane = new OriginalPanel(mainFrame);
            tabbedPane.addTab("Original Image", null, originalPane, "Original Image");

        targetPane = new TargetPanel(mainFrame);
            tabbedPane.addTab("Target Image", null, targetPane, "Target Image");
        resultPane = new JPanel();
        tabbedPane.addTab("Result Images", null, resultPane, "Result Images");
            //Add the tabbed pane to this panel.
            //Uncomment the following line to use scrolling tabs.


public class OriginalPanel extends JPanel implements ActionListener {

    private Main mainFrame;

    private JScrollPane scrollPane;
    private ImagePanel imagePane;
    private JPanel buttonPane;

    public OriginalPanel (Main mainFrame) {
        this.mainFrame = mainFrame;
        imagePane = new ImagePanel();
        imagePane.setPreferredSize(new Dimension(200, 200));
        this.setLayout(new BorderLayout());
        this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        // Create a scroll pane and add the image to it.
        scrollPane = new JScrollPane();
        // Create the control panel for the picture
        buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.PAGE_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 30, 10, 0));
        JButton b = new JButton("Load Image");
        this.add(scrollPane, BorderLayout.CENTER);
        this.add(buttonPane, BorderLayout.LINE_END);
dechAuthor Commented:
Sorry for the extra bits which are not required.

I've left out the other methods of each of these files which I do not feel is relevent. Request them if I've missed something out.
the preferred size needs to be set to the size of the image

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dechAuthor Commented:
THANK YOU. That worked

Followup Questions:

1) If I dont set the preferredsize, what size does it use?

2) What is the difference between setSize and setPreferredSize
> 1) If I dont set the preferredsize, what size does it use?

not sure off the top of my head, and probably depends on the UI. use getPreferredSize() to check.

> 2) What is the difference between setSize and setPreferredSize

setSize sets the actual size of the panel.
But if a layout manager is in use then it handles setting the size.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.