Dynamically resize textarea to fit content - java

Here is a textarea I have put in my program using netbeans. The first image shows what the textarea looks like when I run the program. The second image is show the textarea after I press a button that adds about 50 "Hello world" strings to the text area - only the first 6 get shown. I need the textarea's height to dynamically increase to fit all these strings. Anyone know how to do it?
Edit: I dont want scrollbars so using a JScrollPane is not an option.

I wouldn't use JTextArea for anything except prototyping. It has the bitter functionality of notepad. But it's up to you.
I would use a JEditorPane or JTextPane. I know you can't size it based on character size but that's for the best. For word wrap, you can do setContentType("text/html"); and wrap the text in <'p><'/p> tags.
(Note you still use JScrollPane for scrolling. In fact, the scroll pane works for any component)
See the differences: http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

Try this http://java-sl.com/tip_text_height_measuring.html
I haven't tried it with JTextArea but suppose it should work

Include the JTextArea inside a JScrollPane
JTextArea textArea = new JTextArea();
JScrollPane scrollArea = new JScrollPane(textArea);
This will dynamically change the text area based on if scrolling is needed or not

Related

How to use scrollbar for a JTextField in JAVA using eclipse GUI drag and drop tool?

I am using the eclipse GUI drag and drop tool. I have a JTextField in my program which shows content from a line in a textfile. Now, I want to add a horizontal scrollbar underneath the JTextField so that the user can view the content no matter how long it is.
I have the scrollbar and JTextField ready in my JFrame. Can someone provide me with guidance on how to link them please?
Don't try to use a JTextField and a JScrollBar.
Instead, use a JTextArea which is designed to work with a JScrollPane:
JTextArea ta = new JTextArea(1, 10);
ta.getDocument().putProperty("filterNewlines", Boolean.TRUE);
JScrollPane sp = new JScrollPane( ta );
sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
You need to reserve space for the scrollbar otherwise it will paint over top of the text. The scrollbar will be activated when required.
This "filterNewLines" property just removes newline characters from the text so only a single line of text can be displayed in the text area.

Enable ScrollPane when components exceed inside a text area

Before anything else there might be some people who already asked this question. However, i am certain that I couldn't google it. Anyway, I have a scrollPane which has a viewPortView of textArea. My question is I would like to show my scrollpane when i insert numerous components inside my textArea. How am i supposed to do this? I have no idea and I'm not that expert with Javax Swing.
Code goes like this:
textArea = new JTextArea();
scrollPane = new JScrollPane();
textArea.setBounds(0,50,520,550);
textArea.setBackground(Color.DARK_GRAY);
scrollPane.setBounds(textArea.getBounds());
scrollPane.setViewportView(textArea);
thanks for the help!
My question is I would like to show my scrollpane when i insert numerous components inside my textArea.
A text area displays text, not components. The scrollbars will appear automatically when you actually add text to the text area.
textArea.setBounds(0,50,520,550);
Don't use setBounds. Swing was designed to be used with layout managers. In particular a JScrollPane will only work properly when you use layout managers.
//textArea = new JTextArea();
textArea = new JTextArea(5, 20);
When you create a JtextArea use code like the above. This will allow the text area to determine its own preferred size. Then scrollbars will appear once you add more than 5 rows of text.
Read the section from the Swing tutorial on How to Use Text Areas for more information and working examples. Keep a link to the tutorial handy for all Swing basics.
Just for information,
If you have multiple lines in your text area, the scroll bar is by default scrolled to the end of the text area. To keep the lines in the text area wrapped and scroll bar to the top of the text area, following code would help
textArea .setWrapStyleWord(true);
textArea .setLineWrap(true);
DefaultCaret caret = (DefaultCaret) textArea .getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

Swing JTextArea: Resize Dialog properly after automatic LineWrap

I hope I did not miss some duplicate question, I feel like this should be trivial!
Anyway, I have a JTextArea with text in it, with automatic line wraps:
public PleaseResize(){
super();
Container cp = this.getContentPane();
JTextArea area = new JTextArea();
area.setColumns(20);
area.setLineWrap(true);
area.setEditable(false);
area.setWrapStyleWord(true);
area.setText("Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint an curious volume of forgotten lore.");
cp.add(area, BorderLayout.CENTER);
cp.add(new JButton("Hallo"), BorderLayout.SOUTH);
this.pack();
}
I want the text area to resize vertically to display the whole text... but it does not. It resizes happily if I choose the line breaks via pushing in \n, but with automatic wrapping, it just remains the size it is.
I somehow feel like I am missing something totally obvious...
Edit: for clarification:
On creation-time, I do not know how many lines the text will have (due to the automatic linebreaks happening). I am receiving Text via an XML-file, and it can vary between 0 and about 30 lines after wrapping. I have the vertical space to display everything, but I do not want to scroll or to have a huge, white area when there is no or only a little text to display.
Edit 2:
After rephrasing the question, they key to the problem I was facing was apparently not resizing the JTextArea, but making sure the dialog knows how big it is!
So, here is the link back to the solution I ended up using: An automatic resizing Text above a button?
You need to wrap JTeatArea with JScrollPane like next new JScrollPane(area); and then add JScrollPane to your JFrame.
Also you create JTextArea with empty constructor, use JTextArea(int rows, int cols) to specify count of rows and columns.

How to set columns in a JTextArea to show text

I'm using (in a JPanel with GridLayout) some JTextAreas (with Editable=false) to show some text after a query to a database XML.
JTextArea obj = new JTextArea();
obj.setColumns(37);
obj.setText(r.getProtocolloList().get(i).getOggetto());
The problem is that this text can be quite long and it is showed all in a single line so that the user has to scroll the horizontal JScrollPane to read the rest.
I thought that setting the columns the line would be restricted so that the text would be showed in different lines. But nothing happens.
Is there a way to get that?
Thanks
Use the setLineWrap(); method:
obj.setLineWrap(true);

Disable Word wrap in JTextPane

I can't find an easy way to turn off word wrap in a JTextPane. I can't use JTextArea because I need different colors for different text. I have these big ugly lines that get uglier with word wrap turned on.
JTextArea has a setLineWrap() method, but I can't find it for JTextPane. Why?
Okay, I found an easy solution. Put the JTextPane into the center of a JPanel with a Border layout. Then, put the JPanel into a JScrollPane.
So, the hierarchy looks like this:
JScrollPane
JPanel (w/ Border Layout)
JTextPane
JScrollPane contains everything below it and the JTextPane is inside of everything above it.
I'm not sure why this works, but it does.

Categories