将 html 文档加载到 JTextPane 使我的编辑器在粘贴时表现非常糟糕

发布于 2024-11-19 20:06:36 字数 833 浏览 2 评论 0原文

我正在制作编辑器。我正在使用以下代码将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2024-11-26 20:06:36

如果您想在文本编辑器中“打开”html 文档,则应该使用 JEditorPane 和 JScrollPane 组合(如果需要)。这是一个示例代码(它需要 try/catch 块):

private void visualiserLog() {
    JEditorPane docEP = new JEditorPane();
    docEP.setEditable(true);
    File f = new File(/path/to/file.html);
    java.net.URL fileURL = null;
    try {
        fileURL = f.toURI().toURL(); // Transform path into URL
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    try {
        docEP.setPage(fileURL); // Load the file to the editor
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    // Initialize scroll pane (if you need it)
    JScrollPane docSP = new JScrollPane(docEP, 
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    docSP.setPreferredSize(new Dimension(800,700));

    // Set up a frame to layout the editor panel
    JFrame frame = new JFrame("HTML File");
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.setBounds(0,0,800,700);
    // If you don't use ScrollPane, you must swap docSP for docEP
    frame.getContentPane().add(docSP,BorderLayout.CENTER); 
    frame.setVisible(true);
}

我认为这可以正常工作以将文件设置到编辑器中。之后,您应该放置复制函数和必要的侦听器。

问候!

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):

private void visualiserLog() {
    JEditorPane docEP = new JEditorPane();
    docEP.setEditable(true);
    File f = new File(/path/to/file.html);
    java.net.URL fileURL = null;
    try {
        fileURL = f.toURI().toURL(); // Transform path into URL
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    try {
        docEP.setPage(fileURL); // Load the file to the editor
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    // Initialize scroll pane (if you need it)
    JScrollPane docSP = new JScrollPane(docEP, 
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    docSP.setPreferredSize(new Dimension(800,700));

    // Set up a frame to layout the editor panel
    JFrame frame = new JFrame("HTML File");
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.setBounds(0,0,800,700);
    // If you don't use ScrollPane, you must swap docSP for docEP
    frame.getContentPane().add(docSP,BorderLayout.CENTER); 
    frame.setVisible(true);
}

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文