title: Add a JScrollBar to a JTextArea
body: <p>I am trying to make a GUI server-to-client message program.  I wrote the network side, and that is irrelevant to the problem.  I am trying to make a JTextArea scroll with a JScrollBar.  How would I do this?  Here is my code for the client (with most of the networking code removed):</p>
  
  <pre><code>public class MyClient extends JFrame
  {
  
      public Client client;
      public static Scanner scanner;      
      public JTextField textField;
      public JLabel label;        
      public static String string;        
      public static JTextArea textArea;           
      public String username;                 
      public JScrollBar scrollBar;        
      public JScrollPane scrollPane;
  
      public MyClient()
      {                   
          setTitle("Client");
          setSize(800, 600);
          setLocationRelativeTo(null);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setLayout(new FlowLayout());                    
          textArea = new JTextArea("");           
          scrollBar = new JScrollBar();           
          label = new JLabel("Please enter your message");
          add(label);         
          textField = new JTextField(70);
          add(textField);
          textField.addActionListener(new ActionListener()
          {               
              @Override
              public void actionPerformed(ActionEvent e) 
              {                   
                  textArea.append(username + ": " + textField.getText() + "\n");                  textField.setText(null);                    
              }               
          }); 
  
          add(textArea);          
          add(scrollBar, BorderLayout.EAST);          
          string = textField.getText();                       
          scanner = new Scanner(System.in);           
      }
  
       class MyAdjustmentListener implements AdjustmentListener 
       {
  
              public void adjustmentValueChanged(AdjustmentEvent e) 
              {                   
                 label.setText("    New Value is " + e.getValue() + "      ");
                 repaint();                  
              }               
          }
  
      public static void main(String[] args) throws IOException
      {           
          SwingUtilities.invokeLater(new Runnable()
          {
  
              @Override
              public void run()
              {                   
                  MyClient myClient = new MyClient();
                  myClient.setVisible(true);
                  myClient.setResizable(false);                   
              }
  
          });         
      }       
  }
  </code></pre>