title: Dynamically Resize a JScrollPane?
body: <p>I Have two files. One extends JFrame, and another Extends JPanel.
  Whenever I change the size of the frame, whether it be maximizing, dragging, whatever, i want the ScrollPane to fit itself to the current size of the frame. 
  There's more to it, there's a top menubar and a bottom bar as well, but i left those out for simplicity.</p>
  
  <p>Essentially, i want it to work like notepad. </p>
  
  <p>right now, I use a  ComponentListener on the frame that calls a setSize method in the the other class.
  The setSize method is just:</p>
  
  <pre><code>public void resize(int x, int y)
  {
  textA.setPreferredSize(new Dimension(x, y-50));
  
  areaScrollPane.setPreferredSize(new Dimension(x,y-50));
  }
  </code></pre>
  
  <p>also, for reference:</p>
  
  <pre><code>public void componentResized(ComponentEvent e)
   {
       textA.resize(panel.getWidth(),panel.getHeight());
    }
  </code></pre>
  
  <p>FYI, it extends JPanel because of the way I add it to the frame:</p>
  
  <pre><code>panel = (JPanel) this.getContentPane();
  panel.setLayout(new BorderLayout());
  panel.add(textA, BorderLayout.CENTER);
  </code></pre>
  
  <p>so what's the best way to do this?
  Thanks!</p>
  
  <p>Edit: Here's the scrollpane file. It's called textA in my main.</p>
  
  <pre><code>    public class TextArea extends JPanel
      {
  
      JTextArea textA=new JTextArea(500,500);
  
      JScrollPane areaScrollPane = new JScrollPane(textA);
      Toolkit toolkit =  Toolkit.getDefaultToolkit ();
      Dimension dim = toolkit.getScreenSize();
      Dimension dim2=(new Dimension((int)(dim.getWidth()),(int)(dim.getHeight()-120)));
      public TextArea()
      {
          //textA.setLineWrap(true);
          //textA.setWrapStyleWord(true);
          textA.setEditable(true);
          textA.setForeground(Color.WHITE);
  
          textA.setBackground(Color.DARK_GRAY);
          this.setFont(null);
  
  
  
          areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
          areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          areaScrollPane.setMinimumSize(new Dimension(300,300));
          areaScrollPane.setSize(new Dimension(800,800));
          textA.setPreferredSize(dim2);
          areaScrollPane.setPreferredSize(dim2);
          areaScrollPane.setMaximumSize(dim2);
          add(areaScrollPane);
       }
  
  
      @Override
      public void resize(int x, int y)
      {
      textA.setPreferredSize(new Dimension(x, y-50));
      areaScrollPane.setPreferredSize(new Dimension(x,y-50));
      }
  
      }
  </code></pre>
  
  <p>and the main:</p>
  
  <pre><code>    public class JEdit extends JFrame implements ComponentListener
  
      {
      TextArea textA=new TextArea();
      JPanel panel;
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args)
      {
  
         JEdit run=new JEdit();
  
      }
  
      public JEdit()
      {
  
          setTitle("JEdit");
          setLayout(new BorderLayout());
          setSize(1100, 1000);
  
          this.setMinimumSize(new Dimension(100,100));
          //setBackground(Color.BLACK);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          try {
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch (ClassNotFoundException ex) {
             System.out.println("error1");
          } catch (InstantiationException ex) {
              System.out.println("error2");
          } catch (IllegalAccessException ex) {
             System.out.println("error3");
          } catch (UnsupportedLookAndFeelException ex) {
              System.out.println("error4");
          }
  
           panel = (JPanel) this.getContentPane();
           panel.setLayout(new BorderLayout());
           //TopBar top=new TopBar();
          // PositionBar posB=new PositionBar();
           panel.add(textA, BorderLayout.CENTER);
  
  
  
  
         // add(top,BorderLayout.NORTH);
         // add(posB,BorderLayout.SOUTH);
  
  
  
  
          addComponentListener(this);
          setVisible(true);
  
      }
  
  
  
      public void componentResized(ComponentEvent e)
      {
           textA.resize(panel.getWidth(),panel.getHeight());
      }
  
      public void componentMoved(ComponentEvent e) {
      textA.resize(panel.getWidth(),panel.getHeight());
      }
  
      public void componentShown(ComponentEvent e) {
      textA.resize(panel.getWidth(),panel.getHeight());
      }
  
      public void componentHidden(ComponentEvent e) {
           textA.resize(panel.getWidth(),panel.getHeight());
      }
  
  
  
      }
  </code></pre>