Java JScrollPane

I am trying to add a vertical scroll of a text field of java programs. I use this code to create my JScrollPane:

console = my textarea.

I also declare JScrollPane vertical;

vertical = new JScrollPane(console); vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); vertical.setVisible(true); this.add(vertical); 

EDIT:

Type of program:

enter image description here I am new to Java but should not work and add vertical scrollbar to textbox

What am I doing wrong?

Thanks for any help.

+6
source share
2 answers

Here is an example:

enter image description here

 import java.awt.Dimension; import javax.swing.*; public class ScrolledPane extends JPanel { private JScrollPane vertical; private JTextArea console; public ScrolledPane() { setPreferredSize(new Dimension(200, 250)); console = new JTextArea(15, 15); vertical = new JScrollPane(console); vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); add(vertical); } public static void main( String args[] ) { new JFrame() {{ getContentPane().add(new ScrolledPane()); pack(); setVisible(true); }}; } } 
+8
source

I think the official tutorial about JTextArea and JScrollPane describes everything about it, other examples here and here

 mySchroll = new JScrollPane(myTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
+7
source

Source: https://habr.com/ru/post/899292/


All Articles