Java Reference
In-Depth Information
// Insert content
try {
document.insertString(document.getLength(), "Hello, Java", attributes);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
Figure 16-4 shows how a JTextPane would appear with the words “Hello, Java” and a
second insertion displayed.
Figure 16-4. SimpleAttributeSet usage demonstration
The source for the program shown in Figure 16-4 is presented in Listing 16-4.
Listing 16-4. Using a SimpleAttributeSet
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
public class SimpleAttributeSample {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Simple Attributes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
StyledDocument document = new DefaultStyledDocument();
SimpleAttributeSet attributes = new SimpleAttributeSet();
attributes.addAttribute(StyleConstants.CharacterConstants.Bold,
Boolean.TRUE);
attributes.addAttribute(StyleConstants.CharacterConstants.Italic,
Boolean.TRUE);
 
Search WWH ::




Custom Search