Saturday, October 31, 2015

How to add scroll bar

How to use the scroll pane.

at first the scroll bar is invisible but when the text inside becomes long enough, it appears.

-------------------------------------------------------------------------
import javax.swing.*;
import java.awt.BorderLayout;

public class Main extends JFrame{

 Main(){
     JTextArea textarea = new JTextArea("This is JTextArea");

     JScrollPane scrollpane = new JScrollPane(); //Scroll bar
     scrollpane.setViewportView(textarea);

     getContentPane().add(scrollpane, BorderLayout.CENTER);
  }

 public static void main(String[] args) {
     Main frame = new Main();

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setBounds(10, 10, 300, 200);
     frame.setTitle("Title");
     frame.setVisible(true);
 }
}

------------------------------------------------------------------------------