views:

434

answers:

1

Hello everyone,

I have the following problem: I have JScrollPane containing an image. The scrollpane is inside a JPanel together with other components in order to be able to layout the whole thing with a BorderLayout (the scrollpane is the CENTER and I have something in the SOUTH). This JPanel is in a JSplitPane. The JSplitPane is finally inside a JFrame.

JFrame { JSplitpane { JPanel(BorderLayout) { JScrollPane(CENTER) { BufferedImage }

JPanel(SOUTH) {...} } } }

My problem now is that when I resize my frame the image in the scrollPane resizes fine until it reaches the images size. Then the image is stretched which I want to prevent. I tried wrapping the JScrollPane in a JPanel that is layout with a FlowLayout. That works fine for the resizing bit but the scrollbars disappear once I resize the frame.

I overrode the scrollPane's setSize() method and found out that the size of the scrollpane is always set to the image's size once the frame is resized. I don't have a clue how to fix that though.

Every hint is appreciated.

Cheers,

Ben

A: 

I believe you should be good if you call setMaximumSize() on the JScrollPane with the width/height of the BufferedImage (but haven't tried it).

And as a general comment: it's a bad idea to override Swing components just to add children. Instead, create a controller class that creates and composes base components.


I reread your question, and am wondering: is the problem that the image gets stretched, or that the scrollbars are disappearing? If you want to keep the scrollbars on the screen even when there's nothing to scroll, take a look at the setVerticalScrollBarPolicy() and setHorizontalScrollBarPolicy() methods.

kdgregory
Sorry for using the answer button. I'm new to this and most of the time stuff makes sense AFTER you pushed a button...Anyways, I solved my problem and it appeared to me when I wrote the SSCEP. I messed around with the maximumSize and the preferredSize and somehow managed to set the maximumSize always to the preferred size right after initalization which caused the weird behaviour.I'm a little bit wiser now, I guess...Thanks for your help.-Ben
Ben