T
T. Sander
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.Attribute.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);
}
}
}
}
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.Attribute.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);
}
}
}
}