2009-04-17 27 views
0

我在擺弄JTextPane中的HTMLDocument。如果我有這樣的情況:HTMLDocument:Swing是否「優化」span元素?

<html>... 
    <p id='paragraph1'><span>something</span></p> 
    <span id='span1'><span>something else</span></span> 
...</html> 

(額外<span>標籤,以防止搖擺的抱怨,我不能改變葉子的innerHTML),或這種情況

<html>... 
    <p id='paragraph1' /> 
    <span id='span1' /> 
...</html> 

我可以調用HTMLDocument的.getElement()並找到ID爲'paragraph1'的元素,但不是ID爲'span1'的元素。如果我將「span1」的標籤從「span」更改爲「p」,那麼我很好。 WTF在這裏?是否還有另一個HTML元素可以用來代替,這將允許我使用id屬性訪問文檔的特定部分,這不會導致換行符? (跨度將是完美:(哎呀!)

編輯:我認爲解決的辦法是重新審視我想要做的,這是利用這樣的事實,我知道如何使圖形用戶界面+表+在顯示HTML,比我在Swing做更多的事情,所以我會問不同的問題....

回答

1

我不知道搖擺,但

<p style="display: inline;">不換行,在相同<span>

+0

不錯的想法,不幸的是,Swing的HTMLDocument似乎對HTML有限的支持。也許我需要找到另一個GUI元素來顯示我的富文本需求.... – 2009-04-17 16:29:29

0

我看了一下javadoc的HTMLDocument,它指向我到HTMLReader

我在HTMLReader中看不到任何提及的span。也許它只是不知道這個元素。

P可能不是一個很好的替代跨度。 P是塊級元素,跨度是文本級元素(see description of those terms)。也許嘗試沒有屬性的字體(另一個文本級元素)?

1

我完全是這個問題。我的span元素消失了。 而如果我使用div,我可以看到它們。但我當然不需要div元素,因爲它會導致換行。

該死!該死的java。

編輯!

停止按!

找到了答案。至少,這是一個解決它的答案。

我仍然能夠確定我有我的span元素。我將描述我正在做什麼,並提供代碼來說明我是如何做到的。

我想知道插入符號在哪個元素中。因此,此代碼存在於caretUpdate函數中,該函數每次移動時爲其提供插入符號位置。

@Override 
public void caretUpdate(CaretEvent e) 
{ 
    System.out.println("caret event: " + e.toString()); 
    Object source = e.getSource(); 

    if (source instanceof JEditorPane) 
    { 
     JEditorPane jep = (JEditorPane)source; 
     Document doc = jep.getDocument(); 
     if (doc instanceof HTMLDocument) 
     { 
      HTMLDocument hdoc = (HTMLDocument)doc; 
      int pos = e.getDot(); 
      Element elem = hdoc.getCharacterElement(pos); 
      AttributeSet a = elem.getAttributes(); 
      AttributeSet spanAttributeSet = (AttributeSet)a.getAttribute(HTML.Tag.SPAN); 

      // if spanAttributeSet is not null, then we properly found ' a span '. 
      // now we need to discover if it is one of OUR spans 
      if (spanAttributeSet!=null) 
      { 
       Object type = spanAttributeSet.getAttribute(HTML.Attribute.TYPE); 
       if (type !=null && type.equals("dragObject")) 
       { 
        // for our logging, we get the ref, which holds the source 
        // of our value later 
        System.out.println("the value is: " + spanAttributeSet.getAttribute("ref")); 
       }     
      } 
     } 
    } 
} 

編輯!!!

Scratch that ...這幾乎成功......除了在Sun的白癡決定的關鍵將是HTML.Attribute類型。不僅如此,HTML.Attribute的構造函數是私有的,恰好我想要的屬性類型不存在於它們的特權屬性集中。混蛋! 所以,一切都不會丟失......我仍然可以通過枚舉器獲得它..但比它需要的更困難一些。

最後修改!

好吧,我現在明白了。如果屬性是已知類型,則它作爲HTML.Attribute(「type」)的一個實例存儲在AttributeSet中。 否則,它以'String'作爲關鍵字存儲在AttributeSet中。 愚蠢的。但我已經到了那裏。