GUI: Scrollable Text Area

This example uses an output text area that is scroll-bars enabled, so that voluminous text may be viewed in a small area/window.


// GUI example
// Scrollable Text Area
import javax.swing.*;

public class ScrollableTextArea{
   public static void main( String args []){
      // Define name and size of output area
      JTextArea outputArea = new JTextArea (17,20);
      // assign to scroller
      JScrollPane scroller = new JScrollPane(outputArea);
      // write some lines and store in String variable out
      int i;
      String out="";
      for(i=0;i<30;i++){
         out += "This is a demonstration of scrolling, line" + i+"\n";
      }
      // assign out to the output area
      outputArea.setText(out);
      // Display as information message
      JOptionPane.showMessageDialog(null,scroller,"Scrolling Demonstration",JOptionPane.INFORMATION_MESSAGE);
      // terminate execution
      System.exit(0);
   }
}