Java Reference
In-Depth Information
// Insert content
try {
document.insertString(document.getLength(), "Hello, Java",
attributes);
} catch (BadLocationException badLocationException) {
System.err.println("Bad insert");
}
attributes = new SimpleAttributeSet();
attributes.addAttribute(StyleConstants.CharacterConstants.Bold,
Boolean.FALSE);
attributes.addAttribute(StyleConstants.CharacterConstants.Italic,
Boolean.FALSE);
attributes.addAttribute(StyleConstants.CharacterConstants.Foreground,
Color.LIGHT_GRAY);
// Insert content
try {
document.insertString(document.getLength(),
" - Good-bye Visual Basic", attributes);
} catch (BadLocationException badLocationException) {
System.err.println("Bad insert");
}
JTextPane textPane = new JTextPane(document);
textPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textPane);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
In summary, to specify the style of the content, simply set up the attribute set, insert the
content, and then repeat the steps for each bit of content you want to add.
StyleConstants Class
The StyleConstants class is chock-full of helper methods to simplify setting attribute sets. And
you don't need to burrow into the constants of the inner classes of StyleConstants , because the
class makes them available through class constants at the StyleConstants level.
Search WWH ::




Custom Search