Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...