Swing JFrame内容保存到html文件

时间:2014-01-16 06:52:31

标签: java html swing

使用JFrame和拆分窗格显示两个大内容之间的差异。它显示了差异并着色了差异。所以它看起来像一张有色差异的桌子。是否可以将其保存为HTML文件?

JEditorPaneHTMLEditorKit仅使用文本内容将其保存在文件中。我是否需要手动完成差异并在HTML中再次对其进行着色?

有没有办法将表格内容从Swing复制到HTML?

private void saveToHTML(File selectedFile){

     ConfigurationDiffUtil util = new ConfigurationDiffUtil(previous.getRuRO().getContent(), latest.getRuRO().getContent());

     JEditorPane yourEditorPane = new JEditorPane();
     yourEditorPane.setContentType("text/html");
     yourEditorPane.setText(util.getLeftEditor().getText());



    if (selectedFile.isDirectory()) {  

        String deviceFolderName = selectedFile.getPath() + File.separator + escapeFileName(deviceId.getName()); 
         File deviceFolder = new File(deviceFolderName);
         if ( (deviceFolder.exists() && confirmOverwrite(deviceFolderName)) || deviceFolder.mkdir()) {

                String saveFileName = escapeFileName(deviceId.getName())+"."+ "html";
                File dataFile  = new File (deviceFolder, saveFileName);
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(dataFile));


        HTMLWriter hw = new HTMLWriter(out, (HTMLDocument) yourEditorPane.getDocument());
        hw.write();


        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        Log.error(this, "ExportCredentials:credentials file not found",e);

    } catch (BadLocationException e) {
        Log.error(this, "ExportCredentials:credentials file not found",e);
    }
    catch (IOException e) {
        Log.error(this, "ExportCredentials:unable to read the file",e);

    }       
         }
    }

}

现在我已经创建了这段代码片段。这里我设置左窗格内容。 yourEditorPane.setText(util.getLeftEditor()的getText());

将其保存在html文件中。

这里我只能保存html中的文字。来自摇摆的外观(表中的左右内容并显示高亮差异),无法以HTML格式获取。只有文本可以保存到HTML中吗?

1 个答案:

答案 0 :(得分:0)

在Swing应用程序中:

JPanel editorPanel = new JPanel(new BorderLayout());
JideSplitPane splitPane = new JideSplitPane();
splitPane.add(diffUtil.getLeftEditor()));
splitPane.add(diffUtil.getRightEditor()));
editorPanel.add(splitPane);

在Swing中,我得到了一个对话框窗口,在左右窗格中填充了两个内容,差异是彩色的。

在这个面板中,我有一个saveToHTML按钮。点击按钮,我必须将此弹出窗口中显示的内容存储到HTML中。

SaveToHTML按钮onclick,我写了这个函数。

ConfigurationDiffUtil util = new ConfigurationDiffUtil(previous.getRuRO().getContent(),     latest.getRuRO().getContent());

 JEditorPane yourEditorPane = new JEditorPane();
 yourEditorPane.setContentType("text/html");
// yourEditorPane.setText(util.getLeftEditor().getText());
JideSplitPane splitPane = new JideSplitPane();

        splitPane.add(diffUtil.getLeftEditor()));
splitPane.add(diffUtil.getRightEditor()));

        yourEditorPane.add(splitPane);


 if (selectedFile.isDirectory()) {  

    String deviceFolderName = selectedFile.getPath() + File.separator + escapeFileName(deviceId.getName()); 
     File deviceFolder = new File(deviceFolderName);
     if ( (deviceFolder.exists() && confirmOverwrite(deviceFolderName)) || deviceFolder.mkdir()) {

            String saveFileName = escapeFileName(deviceId.getName())+"."+ "html";
            File dataFile  = new File (deviceFolder, saveFileName);
 try {
    BufferedWriter out = new BufferedWriter(new FileWriter(dataFile));


    HTMLWriter hw = new HTMLWriter(out, (HTMLDocument) yourEditorPane.getDocument());
    hw.write();


    out.flush();
    out.close();
} catch (FileNotFoundException e) {
    Log.error(this, "ExportCredentials:credentials file not found",e);

} catch (BadLocationException e) {
    Log.error(this, "ExportCredentials:credentials file not found",e);
}
catch (IOException e) {
    Log.error(this, "ExportCredentials:unable to read the file",e);

}       
     }
}

} Panel from Swing showing difference

i)setText显示html页面中的内容。

 yourEditorPane.setText(util.getLeftEditor().getText());

这一行显示了html中的文本内容。

ii)我尝试将此拆分窗格添加到JEditorPane。

JideSplitPane splitPane = new JideSplitPane();

        splitPane.add(diffUtil.getLeftEditor()));
splitPane.add(diffUtil.getRightEditor()));

        yourEditorPane.add(splitPane);

但html页面中没有显示任何内容。 我需要在splitPane中向HTML页面显示相同的内容。

谢谢。