2012-05-15 61 views
1

我的Swing應用程序包含一個JEditorPane,它可以讓所見即所得的時尚用戶編輯文本,包括粗體,斜體工具欄按鈕,並設置字體等的Java Swing存儲和渲染文本

的內容類型JEdi​​torPane是text/html

問題:用戶希望能夠在編輯器窗格中鍵入製表符,關閉編輯對話框(將HTML文本保存到永久存儲器)並查看其呈現的製表符。但是,HTML將所有空白運行視爲單個空間。

public class NoteTest { 
    public static void main(String[] args) { 
     final JEditorPane editPane1 = new JEditorPane("text/html", "Try typing some tabs"); 
     editPane1.setPreferredSize(new Dimension(400, 300)); 
     JOptionPane.showMessageDialog(null, new JScrollPane(editPane1)); 
     JOptionPane.showMessageDialog(null, new JScrollPane(new JEditorPane("text/html", editPane1.getText()))); // where did the tabs go? 
    } 
} 

在第一JEditorPane中輸入標籤後,我們從它那裏得到的HTML文本,並把它傳遞給第二JEditorPane中,這使得標籤的空間。

如何渲染這些標籤?我應該將用戶輸入的內容保存爲RTF而不是HTML嗎?我應該解析HTML並構建一個包含行和單元格的HTML表格(唉)。或者有什麼方法可以讓擺動將製表符呈現爲製表符?

+1

雖然HTML沒有_tab_東西,即使您在html中使用多個空格,它也會呈現爲單個空格 – Asif

回答

1

如果您使用< PRE> </PRE>標籤爲文本, 爲tab char?

+0

感謝Stanislav,我向HTMLDocument添加了一個樣式表規則來執行此操作。 –

3

爲了更快提供幫助,請發佈SSCCE。此SSCCE不會顯示您描述的行爲。

Text with tab included

typin gn & g之間的 '標籤'。

import java.awt.Dimension; 
import javax.swing.*; 

public class NoteTest { 
    public static void main(String[] args) { 
     final JEditorPane editPane1 = new JEditorPane("text/html", "Try typing some tabs"); 
     editPane1.setPreferredSize(new Dimension(400, 300)); 
     JOptionPane.showMessageDialog(null, new JScrollPane(editPane1)); 
     JOptionPane.showMessageDialog(null, new JScrollPane(new JEditorPane("text/html", editPane1.getText()))); // where did the tabs go? 
    } 
} 

有那個消失年底一些標籤,但是這是有道理的,因爲除非包含在pre元素標籤並不是HTML支持正確。我猜測Swing HTML解析忽略它們是多餘的。

+0

感謝Andrew,那麼如何呈現用戶輸入的選項卡作爲標籤(而不是空格)在關閉它們進入標籤的初始JEditorPane之後? –