Reputation: 13
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
Upvotes: 1
Views: 4066
Reputation: 39606
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.
Upvotes: 0