This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes HTML text on SWING components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "HTML text on SWING components" Watch "HTML text on SWING components" New topic
Author

HTML text on SWING components

Vernon Gibson
Ranch Hand

Joined: Dec 01, 2000
Posts: 35
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?
Thanks
VG
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
This is the formatting documentation that I found for Swing:
http://developer.java.sun.com/developer/onlineTraining/GUI/Swing2/shortcourse.html

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
pane.setStyledDocument(
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
doc.insertString(doc.getLength(),
"Hello World\n", bigStyle);
doc.insertString(doc.getLength(),
"What's up Doc?\n", italicStyle);
doc.insertString(doc.getLength(),
"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: http://www.javaranch.com/ubb/Forum2/HTML/000557.html


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HTML text on SWING components