JButton and HTML

View: New views
2 Messages — Rating Filter:   Alert me  

JButton and HTML

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi at all!
I need a button with html not it. The standard jbutton supports it, but i have following problem:
i use a css-file for formatting, that is contained in a resource-jarfile within the classpath. I already use this technique in combination with a jtextpane. to get it working i had to implement my own Document. therefor i created a subclass of a HTMLDocument and did some modification as shown below.
But i cannot use this with the jbutton, because the label-text of the jbutton is not stored within a document. so i took a look at the BasicButtonUI and recognized, that the clientProperty(BasicHTML.propertyKey) decides how to render html (you have to put a view in there). now i tried, to create a new instance of a HTMLEditorKit and my SoitHTMLDocument and inserted the html-code into the document. after that i created a view with the viewfactory of the editorkit and put it into the clientProperties:

------------------------------
SoitHTMLDocument document = new SoitHTMLDocument();
editorKit.insertHTML(document, 0, text, 0, 0, null);
button.putClientProperty(BasicHTML.propertyKey, editorKit.getViewFactory().create(document.getDefaultRootElement()));
-----------------------------

but it doesnt work, i get a null-pointerexception while painting:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.text.html.BlockView.paint(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonUI.paint(Unknown Source)
    at com.jgoodies.looks.plastic.PlasticButtonUI.update(PlasticButtonUI.java:91)
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)


any ideas?

what about developing a JXButton that has some new features, for example storing the labeltext in a document, like the jtextpane does (and -off course- renders it the same way like the jtextpane does). then one would be able to use the same styleddocuments, that you could use with the jtextpane, not only the html-style. eg if you want to put text on the button, where not every word has the same font. with jbutton you have to use html for this reasons...

thanks in advance
for help with my crazy problem...

Karsten



---- code of my document:
public class SoitHTMLDocument extends HTMLDocument {

 @Override
 public ParserCallback getReader(int pos) {
   Object desc = getProperty(Document.StreamDescriptionProperty);
   if (desc instanceof URL) {
    setBase((URL) desc);
   }
   return new SoitHTMLReader(pos);
  }

  @Override
  public ParserCallback getReader(int pos, int popDepth, int pushDepth, Tag insertTag) {
   Object desc = getProperty(Document.StreamDescriptionProperty);
   if (desc instanceof URL) {
    setBase((URL) desc);
   }
   return new SoitHTMLReader(pos, popDepth, pushDepth, insertTag);
  }

  private class SoitHTMLReader extends HTMLReader {

   public SoitHTMLReader(int offset) {
    super(offset);

    init();
   }

   public SoitHTMLReader(int offset, int popDepth, int pushDepth, Tag insertTag) {
    super(offset, popDepth, pushDepth, insertTag);

    init();
   }

   private void init() {
    registerTag(HTML.Tag.LINK, new SoitLinkAction());
   }

   private class SoitLinkAction extends HiddenAction {

    public void start(HTML.Tag t, MutableAttributeSet a) {
     String rel = (String)     a.getAttribute(HTML.Attribute.REL);
     if (rel != null) {
      rel = rel.toLowerCase();
      if (rel.equals("stylesheet") || rel.equals("alternate stylesheet")) {
       String href = (String) a.getAttribute(HTML.Attribute.HREF);

       URL url = null;
       try {
        url = new URL(href);
       } catch (MalformedURLException mfe) {
        url = Thread.currentThread().getContextClassLoader().getResource(href);
       }
       if (url != null) {
        getStyleSheet().importStyleSheet(url);
       }

      }
     }
     super.start(t, a);
    }

   }
  }
}
[Message sent by forum member 'karsten_soit' (karsten_soit)]

http://forums.java.net/jive/thread.jspa?messageID=288837

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@...
For additional commands, e-mail: jdnc-help@...


Re: JButton and HTML

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you see how this library does it?

http://weblogs.java.net/blog/enicholas/archive/2008/07/introducing_jav.html
[Message sent by forum member 'i30817' (i30817)]

http://forums.java.net/jive/thread.jspa?messageID=288841

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@...
For additional commands, e-mail: jdnc-help@...

LightInTheBox - Buy quality products at wholesale price