I am using a JTextPane
to print out chat messages, implementation:
private HTMLEditorKit kit;
private HTMLDocument doc;
ta = new JTextPane();
ta.setEditable(false);
ta.setContentType("text/html");
sbrText = new JScrollPane(ta);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sbrText.setBorder(null);
doc = (HTMLDocument)ta.getDocument();
kit = (HTMLEditorKit)ta.getEditorKit();
The messages are being inserted like this:
try {
kit.insertHTML(doc, doc.getLength(), "<div style=\"padding-top:10px;
padding-bottom:10px;\" id=\"X\">" + "<div>" + from + " at
" + tid + ":</div>" + "<div style=\"padding-top:4px;" +
align + "\">" + msg + "</div>" + "</div>", 0, 0, null);
} catch (BadLocationException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
Each message (div) gets a unique ID, i want to select a certain ID and update the contents of that html insert.
Is this possible?
It's possible. You can get the document from the
JTextPane
and usegetDefaultRootElement()
to get root of the DOM. Then go through all the childrent and children of children Elements. For each of them usegetAttributes()
and check whether there is ID attribute. Then check the attribute value.When you achieve
Element
with specified ID useHTMLDocument
's method