views:

256

answers:

3

I'm messing about with HTMLDocument in a JTextPane in Swing. If I have this situation:

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

(the extra <span> tags are to prevent Swing from complaining that I can't change the innerHTML of a leaf) or this situation

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

I can call HTMLDocument.getElement() and find the element with ID 'paragraph1' but not the element with id 'span1'. If I change the tag for 'span1' from "span" to "p" then I'm fine. WTF is going on here? Is there another HTML element I can use instead that will allow me to access a particular portion of the document using the id attribute, that will not cause linebreaks? (span would have been perfect :( argh!)

edit: I think the solution is to re-examine what I'm trying to do, which was to leverage the fact that I know how to make GUIs + tables + displays in HTML a lot more than I do in Swing, so I'll ask a different question....

+1  A: 

I don't know Swing, but

<p style="display: inline;"> does not line-break, the same as <span>

Skip Head
Great idea, unfortunately Swing's HTMLDocument seems to be a limited support for HTML. Perhaps I need to find another GUI element to display my rich-text needs....
Jason S
A: 

I took a look at the javadoc for HTMLDocument, which pointed me to HTMLReader.

I don't see any mention of span in HTMLReader. Maybe it just doesn't know that element.

P probably isn't a good replacement for span. P is a block-level element and span is a text-level element (see description of those terms). Maybe try font (another text-level element) with no attributes?

John M
+1  A: 

I have exactly this problem. My span elements disappear. Whereas if i used div i can see them. But of course I don't want div elements, because it causes a line break.

Damn it! Damn java.

Edit!

STOP THE PRESS!!

Found the answer. At least, an answer which fixes it for me.

I was still able to determine that I had my span element. I will describe what I am doing, an d provide the code to how i did it.

I want to know what element the caret is in. So, this code exists within the caretUpdate function, which provides me with the caret position each time it moves.

@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"));
    }     
   }
  }
 }
}

Edit!!!

Scratch that... This almost works... except the idiots at Sun decided the key was going to be of type HTML.Attribute. Not only that, that the constructor for the HTML.Attribute is private, and it just so happens that the attribute type that I wanted doesn't exist within their privileged set of attributes. Bastards! So, all is not lost... I can still get it via the enumerator.. but it is a little more difficult than it needed to be.

LAST EDIT!

Ok, I get it now. If the attribute is of a known type, it is stored in the AttributeSet as an instance of HTML.Attribute("type"). Otherwise, it is stored in the AttributeSet with a 'String' as the key. Stupid. But i've got there.