ScrollPanel unable to scroll

Hi,

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

=======================
Intro
=======================

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.


=======================
Problem
=======================

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
=======================

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

Dech
LVL 2
dechAsked:
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
0
objectsCommented:
try setting the preferred size of your panel either by overriding setPreferredSize() or by calling setPreferrredSize()
0
objectsCommented:
If you're panel just paints the image it may be easier to use a JLabel with an ImageIcon instead.
0
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.

krakatoaCommented:
Can't you post your code?
0
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.

Thanks.
0
objectsCommented:
> Best if I email it out all together.

aint allowed sorry.


Did you try setting the preferred size of your panel?
0
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.
0
objectsCommented:
>  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.
0
dechAuthor Commented:
Thats what I currently have.
0
objectsCommented:
can you just post the code thats creates the tabbed pane, and where you set the preferred size.
0
dechAuthor Commented:


===============================
Main.java (JFrame file)
===============================
    public Main(String name) {
        super(name);
       
        this.setJMenuBar(createMenuBar());
       
        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));
        buttonPane.add(Box.createHorizontalGlue());
       
        // GA Options
        gaButton = new JButton("Set GA Options");
        gaButton.addActionListener(this);
        buttonPane.add(gaButton);
       
        // Space between the buttons
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
       
        // Exit button
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        buttonPane.add(exitButton);
       
        contentPane.add(buttonPane, BorderLayout.PAGE_END);

        // Close all other frames if this frame closes
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Display the window.
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screenSize);
        this.setVisible(true);
    }

===============================
TabbedPane.java
===============================

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

            tabbedPane = new JTabbedPane();

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

        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.
            this.add(tabbedPane);
   
            //Uncomment the following line to use scrolling tabs.
            tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
      }


===============================
OriginalPanel.java
===============================

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();
        scrollPane.setViewportView(imagePane);
       
        // 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");
        b.addActionListener(this);
       
        buttonPane.add(b);
       
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        this.add(scrollPane, BorderLayout.CENTER);
        this.add(buttonPane, BorderLayout.LINE_END);
    }
0
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.
0
objectsCommented:
the preferred size needs to be set to the size of the image
0

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
0
objectsCommented:
> 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.
0
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
Java

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.