Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JScrollPane

I'm trying to add a Vertical scrolling my java programs textarea. I am using this code to create my JScrollPane:

console = my textarea.

I am also Declaring JScrollPane vertical;

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

EDIT:

View of program:

enter image description here I'm new to Java but shouldn't that work and add a Vertical scroll bar to my textarea

What am I doing wrong?

Thanks for any help.

like image 427
Duncan Palmer Avatar asked Oct 14 '11 11:10

Duncan Palmer


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);
        }};
    }
}
like image 96
Eng.Fouad Avatar answered Oct 14 '22 03:10

Eng.Fouad


I think that in official tutorial about JTextArea and JScrollPane is described everything about that, another examples here and here

mySchroll = new JScrollPane(myTextArea, 
    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
like image 22
mKorbel Avatar answered Oct 14 '22 04:10

mKorbel