/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class StyledSample {
public static void main(String args[]) {
String BOLD_ITALIC = "BoldItalic";
String GRAY_PLAIN = "Gray";
JFrame frame = new JFrame("Simple Attributes");
Container content = frame.getContentPane();
StyledDocument document = new DefaultStyledDocument();
Style style = (Style) document.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setBold(style, true);
StyleConstants.setItalic(style, true);
document.addStyle(BOLD_ITALIC, null);
// style = document.getStyle(StyleContext.DEFAULT_STYLE);
// StyleConstants.setBold(style, false);
// StyleConstants.setItalic(style, false);
// StyleConstants.setForeground(style, Color.lightGray);
// document.addStyle(GRAY_PLAIN, null);
style = document.getStyle(BOLD_ITALIC);
// Insert content
try {
document.insertString(document.getLength(), "Hello Java\n", style);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
style = document.getStyle(GRAY_PLAIN);
// Insert content
try {
document.insertString(document.getLength(),
" - Good-bye Visual Basic\n", style);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
JTextPane textPane = new JTextPane(document);
textPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textPane);
content.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
|