How can I restrict JScrollPanel
view width? I do not want horizontal scroll to happen in any case. I tried to supply own JViewport
but that does not help a lot.
private static class WidthRestrictingViewport extends JViewport {
private Container contents;
public WidthRestrictingViewport(final Container cnts) {
contents = cnts;
}
@Override
public Dimension getPreferredSize() {
if (getParent() instanceof JScrollPane) {
JScrollPane sp = (JScrollPane) getParent();
return new Dimension(sp.getWidth(), contents.getHeight());
}
return super.getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
}
And code that create JScrollPane
Container fpdp = ....
JScrollPane sp = new JScrollPane();
sp.setViewport(new WidthRestrictingViewport(fpdp));
sp.setViewportView(fpdp);
contents
instance that I supply takes its maximum width, however I want its width to be no more than JScrollPane
width so horizontal scrolling wont happen. What am I doing wrong?
Sorry for my poor explanations. I found a way to do that. Thanks to JScrollpane needs to shrink its width.
One more problem I faced is wrong layout manager in ScrollablePanel
. That was FlowLayout
that screwed all things up. I did not understand it at once and thought that problem is with Scrollable
interface usage.
So I just took ScrollablePanel
and set its layout manager to BorderLayout
and that is all fine now!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With