wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Scrolling BIG Images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Scrolling BIG Images" Watch "Scrolling BIG Images" New topic
Author

Scrolling BIG Images

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
I have a big image and a limited area to display it. So, I wanted to scroll the Image in a JScrollPane. I had a look at JDK sample source code and imitated it in my program to suit my needs. But, no ScrollBars appear no matter what I try.
What I did is this:


I make an instance of this class and then add it to a panel. A section of the image appears but no scroll bars to scroll it.


With Best Regards,
Shyam Prasad Murarka
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Does it work any better if you dispense with p and just do:



bitguru blog
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Brian,
Nopes, it just remains as dead as ever. But, while searching through the archives, I came across this discussion http://www.coderanch.com/t/329600/GUI/java/Panel-ScrollPane . Here too they face a similar problem to mine. Seems like we are not allowed to add panels to a JScrollPane if we set the layout of the panel as null. But I do need the null layout.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Shyam Prasad Murarka:
Seems like we are not allowed to add panels to a JScrollPane if we set the layout of the panel as null. But I do need the null layout.


ok, you're confusing me here because the panel you added to your scroll pane has a BorderLayout, not null layout. Or at least it does in the code you posted above.

I'm not sure I would say we are not allowed to add containers with a null layout to a scroll pane, but I agree that it is unlikely to work. Without a layout manager, the container won't be able to tell the scroll pane how big it wants to be. Or rather it will, but it won't want to be as big as one would probably like it to be.

As far as you needing to use a null layout manager, I doubt it. You may be an exception, but the overwhelming majority of people who think that way are mistaken.

If you really want the panel in the scroll pane to have a null layout manager, then you might be able to get it to work to your satisfaction if you set its size manually. So try doing something like calling setPreferredSize() or overriding getPreferredSize(). Though I'm not sure why this is better then just letting a real layout manager set the size to be exactly the size of the label's icon plus whatever borders may be set.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Brian,
I meant that the panel holding the JScrollPane has a null layout.
I already tried setting the size of the panel using setPreferredSize() method but it didn't work.
I don't know how big the picture's going to be because I need to receive it from the server. So I have to set a pre-determined size for the panel and the picture is the only component in the panel. So if I dont use a null layout the panel will just shrink into nothing. Do you have any suggestions on which layout I could use. I was wondering about GridBagLayout but I find it very irksome.
[ January 13, 2007: Message edited by: Shyam Prasad Murarka ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Shyam Prasad Murarka:
I meant that the panel holding the JScrollPane has a null layout.


ok, then the problem you are having has nothing to do with any of the code you have posted before. Indeed, your ImageScroller class works fine as is:



Running this scrolls the image just fine. On the other hand, I don't notice any difference between using ImageScroller vs. putting the label directly in a JScrollPane (see the commented line).


So you are adding an ImageScroller to a panel with null layout. As I said before, this sounds like a bad idea to me, and indeed it seems to be responsible for the problems you are having. Still, it should be possible for you to force the ImageScroller to actually appear. This should happen (probably) if you call your ImageScroller's setBounds() method. Are you doing that?

I can't quite follow your justification for using the null layout. You're getting your image from a server and you don't know how big it is. Ok, but why does that cause your panel to shrink, and how could a null layout help things?
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Brian,
Yes, I knew that the ImageScroller was a waste of code, anyways I removed it now. I also set the parent container of the JScrollPane to be a GridLayout but it still does not solve the problem.
I actually had four panels in the frame. A MapZone, Chat Zone, UsersZone and SelfUserZone. Now, because of the GridLayout all these zones have been assigned the same size whereas I want them to be placed in varying sizes.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Shyam Prasad Murarka:
I also set the parent container of the JScrollPane to be a GridLayout but it still does not solve the problem.


Well I don't know what to tell you. Which layout manager you choose depends on how you want it to look. As always with scroll panes, choose a layout manager that will stretch your pane so it has some size. (i.e. GridLayout, the CENTER of BorderLayout, or GridBagLayout with the proper constraints, thought personally I almost never use GridBagLayout.)

There are certainly ways to lay out four panes of varying sizes with the traditional layout managers. (Or you could even create your own layout manager.)

And if you really want to use null layout, which I do not recommend, it should still (mostly) work as long as you're careful. Something like this:

In any case, it sounds like you are having layout problems, not scroll pane problems.

[edit: "I don't know what to tell you" replaces "I don't want to tell you." This is perhaps the worst typo I have ever made. Sorry about that.]
[ January 14, 2007: Message edited by: Brian Cole ]
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Brian,
I still haven't solved the problem.
Yet, THANK YOU very much. Because of your continued persistence on ot using the null layout, I decided to do away with it.
I went back to the Java Tutorial to look at the available layouts and to my utmost pleasure I found SpringLayout which I had utterly forgotten about. It has helped me to layout me compnents just the way I wanted it.
Coming back to my problem, the scrollbars still do not appear.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Brian,
Hey, I just solved it. Like you told, I set the preferred size for the scroll pane, and lo and behold the scroll bars appear. I don't understand why this happens though.
If you do, could you please explain it to me?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scrolling BIG Images
 
Similar Threads
Horizontal Scroll Bar
JScrollPane problem
Background image not loading on JPanel
Urgent: Is signed applet really necessary to read local files ?
JScrollBar and JButton display problems