2014-01-16 69 views
0

使用JFrame和拆分窗格来显示两个大内容之间的区别。它显示了差异并着色了差异。所以它看起来像一张有着不同颜色的桌子。可以将它保存为HTML文件吗?Swing JFrame内容保存到html文件

JEditorPaneHTMLEditorKit仅使用文本内容将其保存在文件中。是否需要手动完成差异并在HTML中重新着色?

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

私人无效saveToHTML(文件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?

+0

*“它显示差异并着色差异。”*如何?请注意,颜色(针对差异)可能会呈现为HTML。 'JEditorPane'和'JTextPane'支持(简单的)HTML渲染。 –

+0

为了更好地提供帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)。 –

+0

我有java代码来计算差异并绘制颜色。所以JFrame包含带有颜色差异的分割面板。现在我想将它保存到一个完全相同内容的html文件中。 – user1631072

回答

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页面。

谢谢。