optional
optional

Reputation: 283

JScrollPane not working on my JTextArea?

I'm not very familiar with java GUIs and layouts but I gotta admit, I didn't think it would be this tricky! I'm not quite sure what I'm doing here.

I merely want to add 2 textareas ontop of each other and add a jscrollpane to each of them. But I can't get the JScrollPanes to work. Here's how I currently got it.

public class Class extends JFrame {

     public Class() {
    super("Title");

    getContentPane().setLayout(
            new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setResizable(false);

    JTextArea window1 = new JTextArea("text");
    window1.setEditable(false);
    window1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    window1.setPreferredSize(new Dimension(200, 250));
    window1.setLineWrap(true);

    add(window1);

    JScrollPane scroll = new JScrollPane(window1);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    add(scroll);

    JTextArea window2 = new JTextArea();
    window2.setEditable(true);
    window2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    window2.setPreferredSize(new Dimension(100, 50));
    window2.setLineWrap(true);
    add(window2);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}
}

It looks good, there's a box with "text" and right of it there's the scrollpane. Below them both there's the second JTextArea (no scrollpane yet). The problem is that when I write text in window 1 and the text goes below, outside of the jtextarea, I expected the JScrollPane to actually be scrollable so I could scroll down to see the text at the bottom but nothing happens when I press it (and it doesn't change in size or anything either). Anything noticable I've missed?

Upvotes: 1

Views: 6221

Answers (2)

alex2410
alex2410

Reputation: 10994

You add your JTextArea to JScrollPane you needn't to add it to your JFrame. Also setPreferedSize() use for JScrollPane, instead of directly to JTextArea, because your JTextArea will be not scrollable. I have changed your code, try it:

public class Class  extends JFrame {
    public Class () {
        super("Title");

        getContentPane().setLayout(
                new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        setResizable(false);

        JTextArea window1 = new JTextArea("text");
        window1.setEditable(false);
        window1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        window1.setLineWrap(true);


        JScrollPane scroll1 = new JScrollPane(window1);
        scroll1.setPreferredSize(new Dimension(200, 250));
        scroll1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        add(scroll1);

        JTextArea window2 = new JTextArea();
        window2.setEditable(true);
        window2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        window2.setLineWrap(true);
        add(window2);

        JScrollPane scroll2 = new JScrollPane(window2);
        scroll2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroll2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll2.setPreferredSize(new Dimension(100, 50));
        add(scroll2);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String... s){
        new Class();
    }
}

Upvotes: 2

Masudul
Masudul

Reputation: 21961

The problem is that when I write text in window 1 and the text goes below, outside of the jtextarea, I expected the JScrollPane to actually be scrollable


JTextArea has been included to JScrollPane. You should not add window1.

add(window1);// Remove this.

JScrollPane scroll = new JScrollPane(window1);//here you added window1
add(scroll);// This statement also added the JTextArea 
            //and do the operaton of above add.

Upvotes: 2

Related Questions