Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HTML text on SWING components  RSS feed

Vernon Gibson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been lead to believe that HTML formatting can be used on SWING components by setting the text property to an HTML string similar to the following: "<html>bold text".
I have not been able to get this to work. Does anyone have any ideas?
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the formatting documentation that I found for Swing:

JTextPane and DefaultStyledDocument
The JTextPane component provides support for multi-attributed text. No longer are you restricted to the single color or font limitations of TextArea. With the help of a DefaultStyledDocument for its model, and a good understanding of the javax.swing.text package, you are well on your way to creating the next word processor or language-sensitive editor.
Creating a JTextPane for complex text display requires two simple steps. A third optional step is generally used also.
Create a DefaultStyledDocument to model for the data
DefaultStyledDocument doc
= new DefaultStyledDocument();
Create a JTextPane using the DefaultStyledDocument
JTextPane pane = new JTextPane (doc);
Place the JTextPane into a JScrollPane
JScrollPane scrollPane = new JScrollPane(pane);
Once you have created your document, you can make various AttributeSet objects to describe the content style:
static final String NORMAL = "Normal";
static final String ITALIC = "Italic";
static final String BIG = "Big";
// Setup initial style set
Hashtable paraStyles;
paraStyles = new Hashtable();
SimpleAttributeSet attr =
new SimpleAttributeSet();
paraStyles.put(NORMAL, attr);
attr = new SimpleAttributeSet();
StyleConstants.setItalic(attr, true);
paraStyles.put(ITALIC, attr);
attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, 36);
paraStyles.put(BIG, attr);
You can also fill up the JTextPane, associating an attribute set with each element in the StyledDocument:
// Clear out current document
doc = new DefaultStyledDocument());
// Get the NORMAL Style
AttributeSet defaultStyle =
(AttributeSet) paraStyles.get(NORMAL);
// Get the ITALIC Style
AttributeSet italicStyle =
(AttributeSet) paraStyles.get(ITALIC);
// Get the BIG Style
AttributeSet bigStyle =
(AttributeSet) paraStyles.get(BIG);
// Insert into pane
"Hello World\n", bigStyle);
"What's up Doc?\n", italicStyle);
"Boring...\n", defaultStyle);

Changing Styles
Then, at the appropriate time, you can use either the various methods shown in the following table of StyleConstants to change the style of the selected contents within the JTextPane or StyledDocument methods such as setCharacterAttributes(), setParagraphAttributes(), or just plain setLogicalStyle() to change the document characteristics. Just create a SimpleAttributeSet, and configure any attribute you would like.

void setAlignment()
void setBackground()
void setBidiLevel()
void setBold()
void setComponent()
void setFirstLineIndent()
void setFontFamily()
void setFontSize()
void setForeground()
void setIcon()
void setItalic()
void setLeftIndent()
void setLineSpacing()
void setRightIndent()
void setSpaceAbove()
void setSpaceBelow()
void setSubscript()
void setSuperscript()
void setTabSet()
void setUnderline()
StyleConstants methods for changing attributes of currently selected content. You will also need to setCharacterAttributes() or setParagraphAttributes() the JTextPane.
void replaceSelection()
void insertComponent()
void insertIcon() JTextPane methods to replace currently selected content with a String, Component, or Icon
void setLogicalStyle() JTextPane method to changes Style of current paragraph
In some cases, you may want to wrap one of the style changing methods into an ActionListener and make it available on a menu or a button.
To make things easier, most of these adapters have already been created for you. With either of these methods, you won't have to worry about finding the selected text to figure out what to change.
A third method of changing styles allows you to not worry about specific class names, just functionality. The StyledEditorKit class provides a minimal set of text actions as a series of inner classes as shown in the table.

StyledEditorKit.AlignmentAction AlignmentAction (String textAction, int alignment)
StyledEditorKit.BoldAction BoldAction()
StyledEditorKit.FontFamilyAction FontFamilyAction (String textAction, String family)
StyledEditorKit.FontSizeAction FontSizeAction (String textAction, int size)
StyledEditorKit.ForegroundAction ForegroundAction (String textAction, Color color)
StyledEditorKit.ItalicAction ItalicAction()
StyledEditorKit.StyledTextAction abstract parent of others
StyledEditorKit.UnderlineAction UnderlineAction()

For AWT formatting you can look at:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!