Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to use custom tag in HTMLDocument in swings JTextPane ?

Reply
Thread Tools

How to use custom tag in HTMLDocument in swings JTextPane ?

 
 
T. Sander
Guest
Posts: n/a
 
      07-12-2004
Hi !

I want to use a custom tag "button" in a HTMLDocument displayed by a
JTextPane.
The HTML Code looks like :

<html>
<head></head>
<body>
Start of Text
<button>Text for Button !!</button>
End of Text
</body>
</html>


I have two problems with my attached code.
1) how can I avoid that the text between the button tags is displayed
plain as text?`
2) How can I access the text inside the button tags to be displayed on
the
JButton. (see method createComponent) ?

Thanks for any comments

Thilo


###########################
package test;

import java.net.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.HTMLDocument.*;
import javax.swing.text.html.HTMLDocument$HTMLReader.*;
import javax.swing.text.html.HTMLEditorKit.*;

public class Frame1
extends JFrame {
JPanel jPanel1 = new JPanel();
JTextPane jTextPane1 = new JTextPane();

public Frame1() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(500, 500);
frame1.setVisible(true);
}

private void jbInit() throws Exception {
jPanel1.setLayout(new BorderLayout());
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jTextPane1, BorderLayout.CENTER);
jTextPane1.setContentType("text/html");
jTextPane1.setEditable(false);
jTextPane1.setEditorKit(new MyHTMLEditorKit());
jTextPane1.setText(
"<html><head></head><body>Start of Text<button>Text for Button
!!</button>End of Text</body></html>");
}

class MyHTMLDocument
extends HTMLDocument {

URL base;

public MyHTMLDocument() {
super();
}

public MyHTMLDocument(StyleSheet styles) {
super(styles);
}

public HTMLEditorKit.ParserCallback getReader(int pos) {
Object desc = getProperty(Document.StreamDescriptionProperty);
if (desc instanceof URL) {
base = (URL) desc;
}
MyHTMLReader reader = new MyHTMLReader(pos);
return reader;
}

class MyHTMLReader
extends HTMLDocument.HTMLReader {

public MyHTMLReader(int offset) {
super(offset);
registerTag(new HTML.UnknownTag("button"), new BlockAction());
}

}
}

class MyHTMLEditorKit
extends HTMLEditorKit {

public MyHTMLEditorKit() {
super();
}

public ViewFactory getViewFactory() {
return new MyHTMLFactory();
}

public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
MyHTMLDocument doc = new MyHTMLDocument(styles);

doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}

class MyHTMLFactory
extends HTMLFactory
implements
ViewFactory {
public MyHTMLFactory() {
super();

}

public View create(javax.swing.text.Element element) {
HTML.Tag kind = (HTML.Tag) (
element.getAttributes().getAttribute(
javax.swing.text.StyleConstants.NameAttribute));

if (element.getAttributes().getAttribute(HTML.Attribu te.ENDTAG)
!= null) {

if (kind instanceof HTML.UnknownTag &&
element.getName().equals("button")) {
return new ComponentView(element) {
protected Component createComponent() {
String text =
"### how to get the text between the button
tags???? ###";
JButton button = new JButton("Button : " + text);
button.setBackground(Color.RED);

return button;
}
};

}
}
return super.create(element);
}

}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Title Attribute Of An HTML Tag In A JTextPane Adam Polt Java 0 05-08-2007 06:33 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
How to handle <Script> tag in javax.swing.text.JTextPane ? Rawash Java 1 12-10-2005 07:26 AM
SPAN tag doesn't display properly in JTextPane Henry S. Thompson Java 0 05-01-2004 09:52 PM
String into HTMLDocument Ike Java 1 04-05-2004 07:38 PM



Advertisments