[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ScrollPanel unable to scroll

Posted on 2004-11-25
15
Medium Priority
?
290 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:dech
  • 7
  • 7
15 Comments
 
LVL 2

Author Comment

by:dech
ID: 12678037
Update: When I set the scroll bars to always appear, no scrolling is allowed either
0
 
LVL 92

Expert Comment

by:objects
ID: 12678055
try setting the preferred size of your panel either by overriding setPreferredSize() or by calling setPreferrredSize()
0
 
LVL 92

Expert Comment

by:objects
ID: 12678057
If you're panel just paints the image it may be easier to use a JLabel with an ImageIcon instead.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Expert Comment

by:krakatoa
ID: 12678058
Can't you post your code?
0
 
LVL 2

Author Comment

by:dech
ID: 12678074
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
 
LVL 92

Expert Comment

by:objects
ID: 12678075
> Best if I email it out all together.

aint allowed sorry.


Did you try setting the preferred size of your panel?
0
 
LVL 2

Author Comment

by:dech
ID: 12678087
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
 
LVL 92

Expert Comment

by:objects
ID: 12678094
>  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
 
LVL 2

Author Comment

by:dech
ID: 12678100
Thats what I currently have.
0
 
LVL 92

Expert Comment

by:objects
ID: 12678105
can you just post the code thats creates the tabbed pane, and where you set the preferred size.
0
 
LVL 2

Author Comment

by:dech
ID: 12678129


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

Author Comment

by:dech
ID: 12678131
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 12678159
the preferred size needs to be set to the size of the image
0
 
LVL 2

Author Comment

by:dech
ID: 12678215
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
 
LVL 92

Expert Comment

by:objects
ID: 12678263
> 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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question