Credits

Monday, February 15, 2010

Java's Scroll Pane

A scroll pane works as scroll bars particularly vertical and horizontal scrollers for a window with long lines of items.


But a scroll pane in Java works together with other components like text areas or a list. An example below shows a sample program with scroll pane and text area. 

[+/-] show/hide




//import java.awt.LayoutManager;
//import java.awt.*;
/*adding of components to frame without a panel*/
import java.awt.FlowLayout;
import javax.swing.*;
public class GUIQuiz2 extends JFrame{

public GUIQuiz2(){
setSize(220,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel toLabel= new JLabel("To: ",SwingConstants.LEFT);
JLabel fromLabel= new JLabel ("From:",SwingConstants.LEFT);
JLabel messageLabel = new JLabel (" Message:",JLabel.LEFT);
JTextField toField=new JTextField("Receiver's name",15);
JTextField fromField = new JTextField("Sender's name",15);
JTextArea msgArea= new JTextArea("Write your message here.",4,15);
msgArea.setLineWrap(true);
msgArea.setWrapStyleWord(true);
JScrollPane scrollpane=new JScrollPane(msgArea);
FlowLayout flo=new FlowLayout();

setLayout(flo);
add(toLabel);
add(toField);
add(fromLabel);
add(fromField);
add(messageLabel);
add(scrollpane);

setVisible(true);
}
public static void main (String args[]){
GUIQuiz2 myFrame=new GUIQuiz2();
}
}

AddThis