title: How do I properly pack JTextPane/JPanel into JPanel into JScrollPane?
body: <p>Below is the answer specific to my original question, but if you're trying to achieve a similar structure in general, it'd probably be more practical follow camickr's method below. To do it the way I originally wanted, you have to override JPanel and implement Scrollable for the outer container.
  (credit to user Kylar - <a href="https://stackoverflow.com/questions/15783014/jtextarea-on-jpanel-inside-jscrollpane-does-not-resize-properly">JTextArea on JPanel inside JScrollPane does not resize properly</a>)</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import javax.swing.text.*;
  
  public class MyEditor extends JTextPane {
      JFrame window;
      JScrollPane editScroll;
      NumberLines numPane;
      HoldMe please;
  
  public static void main(String args[]) {
      new MyEditor();
  }
  
  MyEditor() {
  
      setOpaque(false);
      setEditorKit(new TextWrapKit());
  
      numPane = new NumberLines();
      numPane.setPreferredSize(new Dimension(50,100));
      please = new HoldMe();
  
      please.setLayout(new BorderLayout());
      please.add(this,BorderLayout.CENTER);
      please.add(numPane,BorderLayout.WEST);
  
      editScroll = new JScrollPane(please);
      editScroll.setPreferredSize(new Dimension(500,500));
      editScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      editScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
  
      window = new JFrame();
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.getContentPane().add(editScroll);
      window.pack();
      window.setVisible(true);
  }
  
  private class HoldMe extends JPanel implements Scrollable{
      public Dimension getPreferredScrollableViewportSize() {
      return super.getPreferredSize(); //tell the JScrollPane that we want to be our 'preferredSize' - but later, we'll say that vertically, it should scroll.
      }
  
       public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
      return 16;//set to 16 because that's what you had in your code.
      }
  
      public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
      return 16;//set to 16 because that's what you had set in your code.
      }
  
      public boolean getScrollableTracksViewportWidth() {
      return true;//track the width, and re-size as needed.
      }
  
      public boolean getScrollableTracksViewportHeight() {
      return false; //we don't want to track the height, because we want to scroll vertically.
   }   
  }
  
  
  private class NumberLines extends JPanel {
  NumberLines() {
          setBackground(new Color(120,120,120));
          setOpaque(false);
          repaint();
      } 
  @Override
  protected void paintComponent(Graphics g) {
      g.fillRect(0,0,this.getWidth(),this.getHeight());
  
  }
  
  }
  
  private class TextWrapKit extends StyledEditorKit {
      ViewFactory defaultFactory=new TextWrapFactory();
      public ViewFactory getViewFactory() {
          return defaultFactory;
      }
  }
  
  private class TextWrapFactory implements ViewFactory {
      public View create(Element elem) {
          String kind = elem.getName();
          if (kind != null) {
              if (kind.equals(AbstractDocument.ContentElementName)) {
                  return new TextWrapView(elem);
              } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                  return new ParagraphView(elem);
              } else if (kind.equals(AbstractDocument.SectionElementName)) {
                  return new BoxView(elem, View.Y_AXIS);
              } else if (kind.equals(StyleConstants.ComponentElementName)) {
                  return new ComponentView(elem);
              } else if (kind.equals(StyleConstants.IconElementName)) {
                  return new IconView(elem);
              }
          }
  
          // default to text display
          return new LabelView(elem);
      }
  }
  
  private class TextWrapView extends LabelView {
      public TextWrapView(Element elem) {
          super(elem);
      }
  
      public float getMinimumSpan(int axis) {
          switch (axis) {
              case View.X_AXIS:
                  return 0;
              case View.Y_AXIS:
                  return super.getMinimumSpan(axis);
              default:
                  throw new IllegalArgumentException("Invalid axis: " + axis);
          }
      }
  
  }
  
  }   
  </code></pre>
  
  <p>EDIT: code replaced with SCCE, sorry for the trouble</p>
  
  <p>Okay, so it's structured like this...</p>
  
  <p>• Top class - extends JTextPane</p>
  
  <p>• Place this JTextPane into a JPanel set to BorderLayout CENTER</p>
  
  <p>• Place another JPanel into the same JPanel set to WEST</p>
  
  <p>• Place the JPanel holding the two into a JScrollPane</p>
  
  <p>• Ready for deployment (nope)</p>
  
  <p>Basically, I can't figure out a way to do this without losing out on something critical. I can get them all together, but I'll lose scrolling. Or I'll get scrolling back, but then lose text wrapping. One time I got wrapping, and technically it scrolled but not as it should have, in otherwords, the scrollpane didn't detect the size of the text. I need the nested JPanel (1st one) to scroll with the JTextPane</p>
  
  <p>The problem area is in the constructor below, sorry it's such a mess but I'm losing it over here trying different things... Probably isn't helping. I'd be so grateful if somebody could help me figure this out. </p>
  
  <p>Thank you for reading.</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import javax.swing.text.*;
  import java.awt.BorderLayout;
  
  public class MyEditor extends JTextPane {
  JFrame window;
  JScrollPane editScroll;
  
  
  public static void main(String args[]) {
      new MyEditor();
  }
  
  MyEditor() {
      setOpaque(false);
      setEditorKit(new TextWrapKit());
  
      JPanel numPane = new JPanel();
      JPanel packEdit = new JPanel();
  
      packEdit.setLayout(new BorderLayout());
      packEdit.add(this,BorderLayout.CENTER);
      packEdit.add(numPane,BorderLayout.WEST);
  
      editScroll = new JScrollPane(packEdit);
      editScroll.setPreferredSize(new Dimension(500,500));        
      editScroll.setViewportView(packEdit);
      editScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      editScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
  
      window = new JFrame();
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.add(editScroll);
      window.pack();
      window.setVisible(true);
  }
  
  private class TextWrapKit extends StyledEditorKit {
      ViewFactory defaultFactory=new TextWrapFactory();
      public ViewFactory getViewFactory() {
          return defaultFactory;
      }
  }
  
  private class TextWrapFactory implements ViewFactory {
      public View create(Element elem) {
          String kind = elem.getName();
          if (kind != null) {
              if (kind.equals(AbstractDocument.ContentElementName)) {
                  return new TextWrapView(elem);
              } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                  return new ParagraphView(elem);
              } else if (kind.equals(AbstractDocument.SectionElementName)) {
                  return new BoxView(elem, View.Y_AXIS);
              } else if (kind.equals(StyleConstants.ComponentElementName)) {
                  return new ComponentView(elem);
              } else if (kind.equals(StyleConstants.IconElementName)) {
                  return new IconView(elem);
              }
          }
  
          // default to text display
          return new LabelView(elem);
      }
  }
  
  private class TextWrapView extends LabelView {
      public TextWrapView(Element elem) {
          super(elem);
      }
  
      public float getMinimumSpan(int axis) {
          switch (axis) {
              case View.X_AXIS:
                  return 0;
              case View.Y_AXIS:
                  return super.getMinimumSpan(axis);
              default:
                  throw new IllegalArgumentException("Invalid axis: " + axis);
          }
      }
  
  }
  </code></pre>
  
  <p>}   </p>