0

By styled, I mean a text whose different parts have different formattings like in Wordpad.

HappyFace
  • 3,439
  • 2
  • 24
  • 43

4 Answers4

2

Swing supports HTML 3.2 in rendering. So the code for a simple unstyled label will be:

JLabel lblUnstyled = new JLabel();
lblUnstyled.setText("Normal unstyled text");

If you want to style your text, you can use HTML 3.2 markup for styling information, so for example:

JLabel lblStyled = new JLabel();
lblStyled.setText("<html><head></head><body>This is text with <i>something in italic</i>.</body></html>");
Torsten Fehre
  • 567
  • 2
  • 7
  • *"Swing supports HTML 3.2 in rendering."* It also supports basic CSS as seen [here](http://stackoverflow.com/questions/14011492/text-wrap-in-joptionpane/14011645#14011645) (used for the body width). See also [How to Use HTML in Swing Components](http://docs.oracle.com/javase/tutorial/uiswing/components/html.html). Swing components that support HTML rendering include `JLabel`, `JButton` (with a few quirks), `JEditorPane`, `JTextPane` .. – Andrew Thompson Feb 20 '17 at 17:15
0

Consider looking at JEditorPane which allows displaying different kinds of content. http://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html

Obviously, you will need to look at a Swing tutorial

SirVirgin
  • 153
  • 1
  • 3
  • 10
0

I would use a JTextPane.

Read the section from the Swing tutorial on Text Component Features for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Try https://github.com/legendmohe/StyleLabel, which extract from jide-oss (http://www.jidesoft.com/javadoc/com/jidesoft/swing/StyledLabel.html)

StyledLabel is a special JLabel which can display text in different styles. It is a component between JLabel and JTextPane. JLabel is simple, fast but has limited features. For example, you can't use different color to draw the text. You may argue JLabel can use HTML tag to display text in different colors. However there are two main reasons to use StyledLabel. First of all, StyledLabel is very fast and almost as fast as JLabel with plain text. HTML JLabel is very slow.

legendmohe
  • 750
  • 7
  • 11