JScrollPane
// Dont forget the packagae
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class JScrollPane_test {
public static void main(String[] args) {
JScrollPane_Test(); // Start the JFrame
}
private static void JScrollPane_Test() {
// A JScrollPane for a JFrame with
JFrame frame = new JFrame("Test JScrollPane");
frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(null);
// A JPanel to Show how to Set a JScrollPane there
JPanel panel = new JPanel(null);
panel.setBounds(200, 200, 200, 200);
Border border = new LineBorder(Color.ORANGE, 4, true); // To see the JPanel for this example
panel.setBorder(border);
// a textArea to show JScrollPane direct in JFrame
JTextArea textArea1 = new JTextArea("Test1 Test2 Test3 Test4 Test5 Test6 Test7 Test8 Test9 Test10 Test11 Test12 Test13 Test14 Test15 Test16 Test17 Test18 Test19 Test20 Test21 Test22 Test23 Test24 Test25 Test26 Test27 Test28 Test29 Test30 Test31 Test32 Test33 Test34 Test35 Test37 Test38 ");
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setFocusable(false);
// a textArea to show JScrollPane in JPanel
JTextArea textArea2 = new JTextArea("Test1 Test2 Test3 Test4 Test5 Test6 Test7 Test8 Test9 Test10 Test11 Test12 Test13 Test14 Test15 Test16 Test17 Test18 Test19 Test20 Test21 Test22 Test23 Test24 Test25 Test26 Test27 Test28 Test29 Test30 Test31 Test32 Test33 Test34 Test35 Test37 Test38 ");
textArea2.setLineWrap(true);
textArea2.setWrapStyleWord(true);
textArea2.setFocusable(false);
// JScrollPane direct in JFrame
JScrollPane scroll1 = new JScrollPane(); // Activate JScrollPane
scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // set verticla
//scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); // or, and also in Horizontal
//scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); // or, and also in Horizontal
scroll1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // set horizontal
scroll1.setBounds(20, 20, 100, 100); // set Bounds if add to a JPanel
scroll1.getViewport().add(textArea1); // Set textArea to JScrollPane by add to a JPanel too
// JScrollPane for JFrame
JScrollPane scroll2 = new JScrollPane(); // Activate JScrollPane
scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // set verticla
//scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); // or, and also in Horizontal
//scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); // or, and also in Horizontal
scroll2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // set horizontal
scroll2.setBounds(20, 20, 100, 100); // set Bounds if add to a JPanel
scroll2.getViewport().add(textArea2); // Set textArea to JScrollPane by add to a JPanel too
panel.add(scroll2); // in this Case add to a JPanel
frame.add(scroll1); // in this Case add to a JFrame
frame.add(panel); //JPanel had to add to JFrame
frame.setVisible(true);
}
}
GRProgram