0

I've a JFrame with multiple panels. One of those should contain a JTextArea of a given size, that can scroll if the text exceeds the area, and that resizes acording to the frame size.

The thing is that when the panel is an instance of JPanel, it looks as I'd like to both in window or fullscreen size, as long as the content doesn't exceed the current area, when it does, it takes the space given to the other components, making them shrink in the frame (here it should show scrollbars instead).

On the other hand, when I use a JScrollPane, I can't manage to make it have the same behaviour of the JPanel, keeping the proper size. Actually it seem to adjust to it's content, and since it's empty, it's just some pixels wide.

How can I achieve what I'm looking for? Thanks in advance

Following the code that keeps the right size (JPanel):

        add(new JPanel(){
            {
                input = new JTextArea(20,20);
                input.setLineWrap(true);
                input.setBackground(Color.WHITE);
                input.setBorder(cb);

                this.setLayout(new BorderLayout());
                //this.setPreferredSize(new Dimension(20,20));
                this.setBorder(eb);
                this.add(input);
            }
        });
camickr
  • 321,443
  • 19
  • 166
  • 288
Dane411
  • 823
  • 1
  • 13
  • 27
  • This should lead you to an answer : http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi – Yassin Hajaj Oct 15 '15 at 14:56
  • So you're saying that a JPanel gives you correct behavior? Or neither do? We'd have to be clear on what the problem is/what the desired behavior is before we could recommend a solution. – markspace Oct 15 '15 at 14:58
  • 1
    `this.add(input);` - should be: `this.add(new JScrollPane(input));` - If that doesn't help then post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Oct 15 '15 at 15:06
  • the last comment worked like a charm! thank you so much! How do I mark this as answered (since there's no answer response but just comments? :D – Dane411 Oct 15 '15 at 15:48

1 Answers1

0

Don't add the text area to the frame.

this.add(input); 

Instead you need to add a JScrollPane containing the text area to the frame:

this.add(new JScrollPane(input)); 
camickr
  • 321,443
  • 19
  • 166
  • 288