Reputation: 161
I add text with JLabel to the JTextPane, then I need to change the text in all JLabel's in the JTextPane. How can I do it?
...
JTextPane pane = new JTextPane();
HTMLEditorKit kit = new CompEditorKit();
HTMLDocument doc = new HTMLDocument();
pane.setEditable(false);
pane.setContentType("text/html");
pane.setEditorKit(kit);
pane.setDocument(doc);
...
kit.insertHTML(doc, doc.getLength(), "Test<object align=\"left\" classid=\"javax.swing.JLabel\"><param name=\"text\" value=\"22\"></object>Test", 0, 0, null);
EditLabels(doc);
...
public void EditLabels(Document doc) {
if (doc instanceof HTMLDocument) {
Element elem = doc.getDefaultRootElement();
ElementIterator iterator = new ElementIterator(elem);
while ((elem = iterator.next()) != null) {
AttributeSet attrs = elem.getAttributes();
Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
if (o instanceof HTML.Tag) {
if ((HTML.Tag) o == HTML.Tag.OBJECT) {
View view = new CompView(elem);
//View view = (View)super.create(elem); //ERROR
//if (view instanceof JLabel)
//{
// ((JLabel) view).setText("NM");
// JLabel label = (JLabel)view;
//}
}
}
}
}
}
View view = new CompView(elem);
((JLabel) view).setText("NM");
error: inconvertible types
required: JLabel
found: View
View view = (View)super.create(elem);
error: cannot find symbol
symbol: method create(Element)
Upvotes: 1
Views: 778
Reputation: 10143
Well, you are mixing two totally different things - JLabel is a separate Swing component that contains text and icon and could be displayed inside any container. And JTextPane is a text editor and its document does NOT contain any separate JLabels, that is why you get "inconvertible types" exception while trying to cast View to JLabel.
View is just a base for various elements created from parsing (in your case) HTML into a structured elements tree.
It might be helpful to read some docs about JTextPane and other similar components: http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
Upvotes: 1