Use CSS to display HTML in jeditorpane

Source: Internet
Author: User

1. Create controls

Jscrollpane htmlscrollpane = new jscrollpane ();

Jeditorpane htmlpane = new jeditorpane ();

Htmlscrollpane. getviewport (). Add (htmlpane );

Htmlpane. seteditable (false );

 

2. Apply htmleditorkit to jeditorpane

Htmleditorkit kit = new htmleditorkit ();
Htmlpane. seteditorkit (KIT );

 

3. Add a style for the style sheet
Stylesheet = kit. getstylesheet ();
Stylesheet. addrule ("table {border-collapse: collapse; Border: solid #000000; border-width: 1px 0 0 1px ;}");
Stylesheet. addrule ("Table caption {font-size: 12px; font-weight: bolder ;}");
Stylesheet. addrule ("Table TD {white-space: Word-wrap; font-size: 10px; Height: 10px; Border: solid #000000; border-width: 0 1px 1px 0; padding: 2px; text-align: Left; Vertical-align: Center }");

 

4. Add a click event for the link

By default, jeditorpane does not respond to link clicks. You should add a listener.

Htmlpane. addhyperlinklistener (New hyperlinklistener (){
Public void hyperlinkupdate (hyperlinkevent e ){
If (E. geteventtype () = hyperlinkevent. eventtype. Activated ){
// If the link is clicked

// Do something

}

}

};

 

5. Create a document and apply it to jeditorpane
Document Doc = kit. createdefadocument document ();
Htmlpane. setdocument (DOC );
String html = generatehtml (); // call other methods to generate an HTML string
Htmlpane. settext (HTML );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.