How do I dynamically resize a text component in Java Netbeans, when the text has filled the component, instead of it having scroll bars?
Asked Answered
L

1

-2

I've done a ton of research regarding this issue, but I haven't had any luck with anything I've found. It's a major roadblock in the application I'm developing.

My application has a Form, which contains a JScrollPane. After adding a JTextArea inside the JScrollPane, I see that when I run the program and type in the JTextArea, it is accompanied by scrollbars when the JTextArea overflows with text. Even if I set the scrollbar policy to 'NEVER', it simply overflows and moves the current text up.

What I would like my JTextArea to do is simply grow in size (vertically/height wise) when the textual content becomes too much to fit in the box. This seems to be a fairly simple concept - something we see, even in MS Paint. When a text object is added, it is a specific size. Then, as text as entered, each new line makes the text field larger, vertically. The text field can also be resized manually by the user. This feature isn't something I will need to do right now.

I have tried getting and setting the size using jTextArea1.getSize(), as well as the row count using jTextArea1.getRows(), line count using jTextArea1.getLineCount(), and the preferred size using jTextArea1.getPreferredSize().

I have tried to use these returned variables to set the size using jTextArea1.setSize(), jTextArea1.setRows(), jTextArea1.setLineCount(), and jTextArea1.setPreferredSize(), none of which have worked. Any advice on what I'm doing wrong?

Lammergeier answered 2/3, 2017 at 6:23 Comment(0)
E
0

Just revalidate() the parent of the text component after adding the new text.

Effort answered 2/3, 2017 at 6:26 Comment(2)
I'm sorry, but your answer isn't very clear... So, would I put this under a "KeyPressed" event, and literally just type something like jTextArea1.revalidate();?...Lammergeier
I think I know what you're talking about, @FredK. I wouldn't revalidate the JTextArea, but the parent of the JTextArea, correct? Also, having said that, if I have the JTextArea inside a JPanel inside a JScrollPane (and I may end up having the JTextArea inside a JScrollPane of its own, and then follow the same hierarchy), would I revalidate the JPanel that it's in directly, or the JScrollPane that contains all of the elements?Lammergeier

© 2022 - 2024 — McMap. All rights reserved.