Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restrict JScrollPane view width

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?

like image 324
michael nesterenko Avatar asked Nov 03 '22 11:11

michael nesterenko


1 Answers

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!

like image 163
michael nesterenko Avatar answered Nov 08 '22 10:11

michael nesterenko