将 html 文档加载到 JTextPane 使我的编辑器在粘贴时表现非常糟糕
我正在制作编辑器。我正在使用以下代码将 html 文档从路径添加到文本编辑器。
try {
filename="filepath";
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filename));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
result = fileData.toString();
jtextpane.setContentType("text/html");
jtextpane.setText(result);
} catch (Exception ex) {
jtextpane.setText(".,1..."+ex.toString());
}
直到我第一次不使用此文件加载时,我的编辑器工作正常。但是添加此代码后,我的粘贴按钮无法正常工作。它正在粘贴到新行中。当我在这种情况下删除 "SETCONTENTTYPE" 时,粘贴工作正常。但我无法删除它。我必须将 html 文件加载到编辑器中。请帮忙。 先感谢您。
I am making editor. I am using following code to add html document from a path to texteditor.
try {
filename="filepath";
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filename));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
result = fileData.toString();
jtextpane.setContentType("text/html");
jtextpane.setText(result);
} catch (Exception ex) {
jtextpane.setText(".,1..."+ex.toString());
}
Till the time when i m not using this file to load at first time my editor is working fine. But after adding this code my paste button is not working properly.It is pasting in new line. when i am removing "SETCONTENTTYPE" in that scenario the paste is working well.but i can't remove it.I have to load html file into editor. Please help.
Thank You in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想在文本编辑器中“打开”html 文档,则应该使用 JEditorPane 和 JScrollPane 组合(如果需要)。这是一个示例代码(它需要 try/catch 块):
我认为这可以正常工作以将文件设置到编辑器中。之后,您应该放置复制函数和必要的侦听器。
问候!
If you want "open" the html document in your text editor, you should use a JEditorPane combined (if it's necessary) which a JScrollPane. Here's an example code (It needs try/catch blocks):
I think that can works properly to set the file into the editor. After that you should put the copy function and the necessary listeners.
Regards!