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

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.

Related

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.

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

Dynamically resize textarea to fit content

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

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.

How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

How exactly is word-wrapping implemented in JTextPane?
I'm trying to understand exactly how it works so that I can modify the behavior. Right now, if I have a standard JTextPane inside a JScrollPane, it will break text at spaces, but not inside long words - if there is a string of text without spaces that is wider than the window, it won't wrap/break and a horizontal scrollbar will appear. As the text width increases, the width of the ParagraphView (via getWidth()) increases to hold the text.
This article by Lapitsky says that LabelView.getBreakWeight() returns View.ExcellentBreakWeight for labels with spaces and View.GoodBreakWeight for labels without spaces (and the code in GlyphView.java seems to confirm this), so why doesn't it break? Is it somehow returning BadBreakWeight instead of GoodBreakWeight? Or is there some layout problem? Or is there a bug?
Here's some code (for your viewing pleasure):
//somewhere inside JPanel or JFrame constructor
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
add(scrollPane);
Note that it still doesn't wrap if I take out the scroll pane and just use the text pane (it just gets clipped as it goes outside the window).
The javadocs for Swing don't seem to go into enough detail on how some objects (like JTextPane, View's, and related objects) work together. Is there any further documentation of the design of such classes, perhaps detailing the purpose of each class and how they all work together? Is it just not publicly available? (Or am I the only one having trouble with things like this? Or is the insufficient documentation limited to things which aren't expected to be dealt with by a typical developer?)
The link about custom wrap (forced wrap and no wrap).
http://java-sl.com/wrap.html
The link about letter wrap
http://java-sl.com/tip_html_letter_wrap.html

Categories