Java Reference
In-Depth Information
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
// Third style for icon/component
Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);
Icon icon = new ImageIcon("Computer.gif");
JLabel label = new JLabel(icon);
StyleConstants.setComponent(labelStyle, label);
// Insert content
try {
document.insertString(document.getLength(), "Ignored", labelStyle);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
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);
}
}
The key line of the source code is the call to insertString() and its style argument:
document.insertString(document.getLength(), message, style);
Figure 15-20 shows how a JTextPane might look like with a few paragraphs of content.
Notice that the content is not restricted to just text; it can have images as well.
Figure 15-20. Sample JTextPane
 
Search WWH ::




Custom Search