在使用安装了JEditorPane.getText()的JEditorPane.getText()时,我试图解决一个不一致的问题。
我可以使用JEditorPane.setText传递包含< br>标记的HTML,并且当我使用getText()时,这些新行正确地显示为< br>。但是当用户在JEditorPane中输入一个新行时,getText()返回一个“/n”字符,而不是一个< br>标记。我的自定义HTML解析器无法区分用户“/n”字符和添加了-seemingly的“/n”字符--以使HTML看起来很漂亮。举个例子:
如果用户输入某种文本,JEditorPane.getText()过程将返回如下内容:
<html>
<head>
</head>
<body>
I've written some text! Indeed so much text that this line is probably
going to word wrap when I run the getText procedure!
And now I just hit enter a few times! I wonder what will happen if it wraps
another time? WHAM.
And I'll hit enter once more for good measure.
</body>
</html>
然而,我希望这会显示为:
<html>
<head>
</head>
<body>
I've written some text! Indeed so much text that this line is probably
going to word wrap when I run the getText procedure!<br><br>And now I
just hit enter a few times! I wonder what will happen if it wraps
another time? WHAM.<br>And I'll hit enter once more for good measure.
</body>
</html>
当用户点击enter时,是否有任何方式将< br>插入到getText字符串中?我的第一次尝试是使用documentFilter,但是文档说我只是大声地在过滤器中使用insertString或filterBypass,因此我不能使用setText (“ ”)路由。在大量阅读之后,我想另一个选择是扩展HTMLEditorKit并重写读取过程?JTextComponents对我来说是新的,所以这已经超出了我的想象。还有其他选择吗?还是资源?
谢谢!
发布于 2014-11-10 09:30:42
您可以使用DocumentListener并跟踪\n插入。在insert上,为插入的\n创建一个虚拟元素,并替换它的外部html (使用HTMLDocument的setOuterElement()方法)。
参见自动替换smiles 这里的示例
https://stackoverflow.com/questions/26824725
复制相似问题