更改 JTextPane 中现有 HTMLDocument 内容的样式
我正在尝试构建一个使用 JTextPane 来显示对话的聊天客户端。我在突出显示用户选择的聊天参与者过去的句子时遇到问题。实现此功能时,我需要坚持使用 HTMLDocument,因为某些内容必须是 HTML。
最好的想法似乎是为参与对话的每个用户使用不同的命名样式。这个想法是,当需要突出显示特定人的文本时,我只需更新他的个人风格,他所说的所有内容都应该像魔术一样突出显示。不幸的是这不起作用。
因此,要添加文本,我使用:
public void addMessage(String from, String message){
HTMLDocument doc = (HTMLDocument) textPane.getStyledDocument();
if(doc != null){
try {
String stylename = "from_" + from;
if(textPane.getStyle(stylename) == null){
LOG.debug("Did not find style. Adding new: " + stylename);
Style nameStyle = textPane.addStyle(stylename, null);
StyleConstants.setForeground(nameStyle, Color.black);
StyleConstants.setBold(nameStyle, true);
}else{
LOG.debug("Found existing style: " + textPane.getStyle(stylename));
}
doc.insertString(doc.getLength(), from + ": ", textPane.getStyle(stylename));
doc.insertString(doc.getLength(), message + "\n", null);
} catch (BadLocationException ble) {
LOG.error("Could not insert text to tab");
}
}
}
到目前为止一切顺利...文本按照我的意愿显示在文本窗格中。但是,当我尝试在将来的某个时刻更新样式表并调用:
public void highlight(String name, boolean highlight){
Style fromStyle = textPane.getStyle("from_" + name);
if(fromStyle != null){
LOG.debug("found style, changing color");
StyleConstants.setForeground(fromStyle, Color.red);
}else{
LOG.debug("fromStyle was NULL");
}
}
..我可以看到样式已找到并且我的代码已执行 - 但屏幕上没有任何变化。
我想询问您是否对我如何尝试解决此问题有任何建议。有没有办法让这个工作与命名样式一起工作,或者我应该采取一些完全不同的方法?
谢谢
I am trying to build a chat client that uses JTextPane to display the conversations. I am having problems with highlighting the past sentences of a chat-participant chosen by the user. When implementing this I need to stick to using HTMLDocument as some of the content has to be HTML.
The best idea seemed to be using a different named styles for each user participating in the conversation. The idea is that when the text of a particular person needs to be highlighted, I just update his personal style and everything he has said should get highlighted as by magic. Unfortunately this does not work.
So to add text I use:
public void addMessage(String from, String message){
HTMLDocument doc = (HTMLDocument) textPane.getStyledDocument();
if(doc != null){
try {
String stylename = "from_" + from;
if(textPane.getStyle(stylename) == null){
LOG.debug("Did not find style. Adding new: " + stylename);
Style nameStyle = textPane.addStyle(stylename, null);
StyleConstants.setForeground(nameStyle, Color.black);
StyleConstants.setBold(nameStyle, true);
}else{
LOG.debug("Found existing style: " + textPane.getStyle(stylename));
}
doc.insertString(doc.getLength(), from + ": ", textPane.getStyle(stylename));
doc.insertString(doc.getLength(), message + "\n", null);
} catch (BadLocationException ble) {
LOG.error("Could not insert text to tab");
}
}
}
So far so good... The text gets displayed in the textPane as I wished. However when I try to update the stylesheet at some future point and call:
public void highlight(String name, boolean highlight){
Style fromStyle = textPane.getStyle("from_" + name);
if(fromStyle != null){
LOG.debug("found style, changing color");
StyleConstants.setForeground(fromStyle, Color.red);
}else{
LOG.debug("fromStyle was NULL");
}
}
..I can see that the style is found and my code gets executed - yet nothing changes on the screen.
I would like to ask if you have any suggestions on how I might try to resolve this issue. Is there a way to make this work with named styles or should I take some completely different approach?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您刚刚修改了
Style
对象,仍然需要使用setCharacterAttributes
。You just modified the
Style
object, you still have to apply the modifiedStyle
usingsetCharacterAttributes
.从那时起,我创建了一个适合我的解决方案。我将在这里为感兴趣的人发布一个功能测试用例。我最终得到了一个解决方案,我更新了 StyleSheet 中的样式类,然后为每个段落元素调用 htmlDocument.setParagraphAttributes 。
I have since created a solution which works for me. I will post here a functioning testcase for anyone interested. I ended up with a solution where I updated the style class in the StyleSheet and then called htmlDocument.setParagraphAttributes for every paragraph element.