Height of JLabel to grow with number of lines - java

I want my JLabel to be the height required for the particular string that is assigned to the text. The text string can can have any number of lines, which use the \n in the string. So if you have a string the "Hello\nit\nis\nme\n" the height of the label will automatically be adjusted to fit the 4 lines.
How to achieve that?

Use proper JTextComponent rather than use HTML formatted text in the JLabel
I think that for "Hello\nit\nis\nme\n" should be disabled JTextArea right JComponent

A JLabel1 would typically ignore the new-line character. To get multi-line text, either use a mutli-line component such as a JTextArea2 or use HTML formatting in the label.
Here is an example of using HTML formatting (screenshot above). The example uses styles (CSS) to cause the line breaks to be inserted automatically. This is much easier than calculating where to insert line-breaks in text blocks manually.
Of course, a JTextArea having methods like setLineWrap(true) & setWrapStyleWord(true) also handles line wrap automatically.

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.

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

Word Wrap in JEditorPane and System Font

I'm wanting to create a large text field for a user to type something in. I also want it to use the default system font to match the look and feel that is expected. So I tried to use a JEditorPane with the default constructor, which uses plain text encoding.
JEditorPane editorPane = new JEditorPane();
editorPane.setText(gettysburgAddress);
The trouble with this is that plain text encoding doesn't wrap to a newline at the end of each word, only when it runs out of characters.
I tried using the HTML encoding, which word wraps:
JEditorPane editorPane = new JEditorPane("text/html", "");
editorPane.setText(gettysburgAddress);
This has the word wrap, but it defaults to a different font than the default for the system (Helvetica on Mac OS X, which I don't want.
How can I get the best of both worlds: word wrap and the system default font? I don't need any special formatting or anything for this, so plain text encoding will do if it is possible.
If all that is needed is a word-wrapped JEditorPane using the system font, and you don't need anything special like stylized text or images, then it's probably best just to switch to a JTextArea, which is a text component for doing just plain text. It doesn't word wrap by default, but it's easy to make it happen:
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true); //Makes the text wrap to the next line
textArea.setWrapStyleWord(true); //Makes the text wrap full words, not just letters
textArea.setText(gettysburgAddress);
If you absolutely must use a JEditorPane for some reason, you'll have to roll up your sleeves and make some changes to the way that the JEditorPane is rendering text. On the plus side, you have several different methods to choose from. You can:
Set the encoding to HTML (which word wraps) and use CSS to specify the font (described here)
Set the encoding to RTF (which word wraps) and modify the font values of the underlying RTFEditorKit (described here)
Create a SimpleAttributeSet and use it when adding strings to specify that they should be displayed in that way (described here)

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

Is there a way to set every other line in a java text area to a different color?

I want to set every other line in a java text area to a second color, is there a way to do this?
example:
<red>1</red>
<blue>2</blue>
<red>3</red>
<blue>4</blue>
the red and blue tags are just for example only.
Neither java.awt.TextArea nor javax.swing.JTextArea support text (or background) decorations. It is one font & one style.
As indicated by Chris, a JTextPane (or JEditorPane) is designed for 'styled text' documents. JEP for example, will handle RTF & (simple) HTML/CSS formatting.

Categories