Anton Yershov
Anton Yershov

Reputation: 57

Why is JScrollPane not working?

When I comment out frame.add(hidden) it only shows the text area. When I don't comment it out, it only shows a large gray box with a grayed out Scrollbar.

import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Panlindrome{
    public Panlindrome(){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Panlindrome?");
        frame.setSize(240,320);
        //frame.setLayout(new GridLayout(3,1));

        JTextArea inputText = new JTextArea(30,1);
        inputText.setLineWrap(true);

        JScrollPane hidden = new JScrollPane(inputText);
        hidden.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        frame.add(inputText);
        //frame.add(hidden);

        frame.setVisible(true);
    }

    public static void main(String[] args){
        Panlindrome check = new Panlindrome();
    }
}

Upvotes: 2

Views: 570

Answers (1)

sam
sam

Reputation: 2125

Don't add inputText to the frame; only add hidden.

The content of the scroll pane is already a child of the scroll pane. If you also try to add it to the frame (actually the frame's content pane, but whatever) as well, it will be in two places at once, which doesn't work.

Upvotes: 5

Related Questions