JTextArea and long text format - java

This is probably very elementary question. I have a JTextArea, for example:
JTextArea area = new JTextArea();
I sometimes insert into this text area long pieces of text (for example using copy-paste method) - this area is for user input.
The problem is, that the text inserts only on one long line instead of adapting the the size of this component. Is there some easy way how to fix this?

Check out...
JTextArea#setLineWrap
JTextArea#setWrapStyleWord
And you might find How to use Text Areas useful.

Hi there is a property to be set if you want this line wrapping.
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
Hope this helps

Related

Splitting a Text in JLabel with <br> [duplicate]

It's been awhile since I asked a question!
I'm developing an application in Java where JLabels are used. Anyway, as you may or may not be able to see from the picture below, the text that says Category Test gets cut off and ends up saying Categor... instead. Is there any way that I can "break" the text once it fills up the width of the label? Here is the image:
What I did
I used a JTextPane like so:
JTextPane text = new JTextPane();
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER);
pane.add(text, c);
Then I added my text, but thanks anyway!
JLabels can't do that by default. But JLabels have some support for html, so a JLabel with the text <html>First Line<br />Second Line</html> would show up on two lines.
If you want a component that can split the lines by itself, take a look at JTextArea.
As I recall, you need to use a JTextArea if you want textwrap. JLabel doesn't do it.
You can at StyledLabel component from JIDE Common Layer open source project at http://java.net/projects/jide-oss/.
The problem with html JLabel approach is it doesn't auto-wrap and about 20 to 40 times slower than a plain JLabel.
The problem with JTextArea or JTextPane approach is it has a weird size issue and is also 20 times slower.
StyledLabel extends JLabel. Automatically line wrapping is just one of the many features it adds. And the performance is as fast as a plain JLabel.
Hope it will help.

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);

Set fonts for individual strings?

So, I have a JTextArea in my program, and I'm appending some text into in.
I'm trying to make just one of the appended Strings bold, without making all of the text in the JTextArea bold. There doesn't seem to be a way, at least not one that I can find, to edit the font of a String without adding it to something like a JLabel (which I'd rather not do).
Anyone know a work around for this?
Thanks for your time.
The best work around: don't use a JTextArea, a component that is made to display simple single fonted text easily. Instead use one of the more robust text components such as JEditorPane or JTextPane. Please have a look at the tutorials:
How to use Editor Panes and Text Panes
Using Text Components
Use a JTextPane instead of JTextArea. By embedding text as html in this component you have more freedom to style the text. See this example:
JTextPane myTextPane = new JTextPane();
myTextPane.setContentType("text/html");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<html>");
stringBuilder.append("<b>bold text </b>");
stringBuilder.append("normal text");
stringBuilder.append("</html>");
myTextPane.setText(stringBuilder.toString());

Text in a JTextField not wrapping around

My original plan today was to learn how to use the JProgressBar, but now I am stuck with a new problem which I was not expecting. So here is how my JFrame looks like right now.
The problem is that the JTextField right under the JTextArea is not wrapping around. It is going beyond the size of the JFrame. I want it to resize relative to the JFrame. How can i do that? I have tried the JLabel but it does the same thing, only it adds ellipses in the end where the text overflows.
Try using a JTextArea if you intend for the field to expand vertically to accommodate the text.
JTextFields are intended for a single line of text.

Categories