title: restrict JScrollPane view width
body: <p>How can I restrict <code>JScrollPanel</code> view width? I do not want horizontal scroll to happen in any case. I tried to supply own <code>JViewport</code> but that does not help a lot.</p>
  
  <pre><code>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();
          }
      }
  </code></pre>
  
  <p>And code that create <code>JScrollPane</code></p>
  
  <pre><code>Container fpdp = ....
  
      JScrollPane sp = new JScrollPane();
      sp.setViewport(new WidthRestrictingViewport(fpdp));
      sp.setViewportView(fpdp);
  </code></pre>
  
  <p><code>contents</code> instance that I supply takes its maximum width, however I want its width to be no more than <code>JScrollPane</code> width so horizontal scrolling wont happen. What am I doing wrong?</p>