jscrollpane

Solutions on MaxInterview for jscrollpane by the best coders in the world

showing results for - "jscrollpane"
Lucia
13 Nov 2018
1// Dont forget the packagae
2import java.awt.Color;
3import javax.swing.JFrame;
4import javax.swing.JPanel;
5import javax.swing.JScrollPane;
6import javax.swing.JTextArea;
7import javax.swing.border.Border;
8import javax.swing.border.LineBorder;
9
10public class JScrollPane_test {
11
12	public static void main(String[] args)   {
13      
14                JScrollPane_Test();					// Start the JFrame
15       
16    }
17
18    private static void  JScrollPane_Test()    {
19    	
20        // A JScrollPane for a JFrame with 
21    	JFrame frame = new JFrame("Test JScrollPane");
22        frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
23        frame.setSize(500, 500);
24        frame.setLayout(null);
25        
26        // A JPanel to Show how to Set a JScrollPane there
27        JPanel panel = new JPanel(null);
28        panel.setBounds(200, 200, 200, 200); 
29        Border border = new LineBorder(Color.ORANGE, 4, true); // To see the JPanel for this example
30        panel.setBorder(border);
31        
32        // a textArea to show JScrollPane direct in JFrame
33        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 ");
34        textArea1.setLineWrap(true);
35    	textArea1.setWrapStyleWord(true);
36    	textArea1.setFocusable(false);
37    	
38    	// a textArea to show JScrollPane in JPanel
39    	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 ");
40        textArea2.setLineWrap(true);
41    	textArea2.setWrapStyleWord(true);
42    	textArea2.setFocusable(false);
43
44    	// JScrollPane direct in JFrame
45        JScrollPane scroll1 = new JScrollPane();		// Activate JScrollPane
46        scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // set verticla
47        //scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 	// or, and also in Horizontal
48        //scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 		// or, and also in Horizontal
49        scroll1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 	// set horizontal
50        scroll1.setBounds(20, 20, 100, 100); // set Bounds if add to a JPanel
51    	scroll1.getViewport().add(textArea1); // Set textArea to JScrollPane by add to a JPanel too
52    	
53    	// JScrollPane for JFrame
54    	JScrollPane scroll2 = new JScrollPane();		// Activate JScrollPane
55        scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // set verticla
56        //scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 	// or, and also in Horizontal
57        //scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 		// or, and also in Horizontal
58        scroll2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 	// set horizontal
59        scroll2.setBounds(20, 20, 100, 100); // set Bounds if add to a JPanel
60    	scroll2.getViewport().add(textArea2); // Set textArea to JScrollPane by add to a JPanel too
61    	
62    	panel.add(scroll2);						// in this Case add to a JPanel
63        frame.add(scroll1); 					// in this Case add to a JFrame
64        frame.add(panel);						//JPanel had to add to JFrame
65        frame.setVisible(true);
66    }
67}
similar questions
queries leading to this page
jscrollpane