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?