Solved

HTML and JEditorPane

Posted on 2001-07-07
8
655 Views
Last Modified: 2012-06-21
Hi!  I made a JEditorPane to display HTML from a test file...but I have 2 problems w/it.

1) Invisible attributes such as HEAD, TITLE, etc.. show up on the JEditorPane when in edit mode (i.e. jep.isEditable() returns true) so I can edit them. This is annoying...I don't care about these tags. I can strip them out of the HTML, but at the very least HEAD and /HEAD still show up. Of course, they go away when I say jep.setEditable(false); ...but that rather defeats the purpose of editing the pane.

Anyone know how I can fix this? Some pics are at http://www20.brinkster.com/xxmariusxx/editorshot.html 

2) When I hit the carriage return, the spacing between lines is too big. See http://www20.brinkster.com/xxmariusxx/spacing.html 

The text whose spacing is correct was read in from the HTML source and consists of BR tags...the incorrect line spacing was what I added...when I generate HTML off of this, the HTML source shows P and /P every time I have a line break, rather than BR. I'd like to just have a simple line break...not paragraph formatting. Anyone know how to do this?

Thanks!

Marius
0
Comment
Question by:xxMariusxx
  • 4
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
Valeri earned 150 total points
ID: 6264278
You should set EditorKit as well.

yourJEditorPane.setEditorKit(new EditorKit());
yourJEditorPane.setContentType("text/html");

EditorKit should be your own class.

It is a snip of my code :
// --------------------------------------------
class EditorKit extends HTMLEditorKit{
  public ViewFactory getViewFactory(){
    return new InitHTMLFactory();
  }

  static class InitHTMLFactory extends HTMLEditorKit.HTMLFactory {

  public View create(Element elem) {
    Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
    if (o instanceof HTML.Tag) {
      HTML.Tag kind = (HTML.Tag) o;
      if(kind == HTML.Tag.P){
         return new ParagraphView(elem)/*javax.swing.text.ParagraphView(elem)*/;
      }else if (kind == HTML.Tag.HEAD || kind == HTML.Tag.META || kind == HTML.Tag.TITLE || kind == HTML.Tag.COMMENT) {
        return new InvisibleView(elem);
      }
      else return super.create(elem);
    }/* else if( o instanceof HTML.UnknownTag ){return new InvisibleView(elem);}*/
    return super.create(elem);
    }

  }

  static class InvisibleView extends View {

  public InvisibleView(Element el) {
     super(el);
     setSize(0,0);
  }

  public float getPreferredSpan(int x) { return 0; }

  public float getMinimumSpan(int axis) { return 0; }

  public void setSize(int width, int height) { super.setSize(0,0); }

  public float getMaximumSpan(int axis) { return 0; }

  public void paint(java.awt.Graphics g, java.awt.Shape s) { setSize(0,0); }

  public java.awt.Shape modelToView(int pos, java.awt.Shape a, Position.Bias b){
   java.awt.Rectangle r = new java.awt.Rectangle();
   return r;
  }

  public int viewToModel(float x, float y, java.awt.Shape a, Position.Bias[] biasReturn) {
   biasReturn[0] = Position.Bias.Backward;
   return 0;
   }
  }

}

Best regards,
Valeri Kronin
0
 

Expert Comment

by:anhphuongnn
ID: 6264415
Hi Marius,

>>2) When I hit the carriage return, the spacing between lines is too big.

my opinion that problem, you can use Shift+Enter
this is simle way.


Bye
PhuongNguyen
0
 
LVL 2

Author Comment

by:xxMariusxx
ID: 6265819
Does not address the 1st half of the question.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 16

Expert Comment

by:Valeri
ID: 6268133
The proposed answer will solve the second half of the question. Did you try it ?

Best Regards,
Valeri Kronin
0
 
LVL 2

Author Comment

by:xxMariusxx
ID: 6269627
Valeri,

Shift+Enter didn't do anything...the cursor doesn't move to the next line.

I've been playing w/your code though...it seems straighforward enough, though I'm having to write a workaround for the generated HTML...the ParagraphView generated by the HTML.Tag.P is great for Swing rendering, but the underlying problem of <p></p> tags are still there...merely hidden in Swing.  When I send the HTML output to the browser, the problem is still there.

Do you know of a way to change the element generated by ENTER from HTML.Tag.P to HTML.Tag.BR?  If so, I'll up the points to 150...if not, I'll just go ahead an award you the 100 points.

Marius
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6273170
HTMLFactory builds by default the following views :
javax.swing.text.html.ParagraphView for HTML.Tag.P and BRView for HTML.Tag.BR.

In your EditorKit , in method create, when the tag is HTML.Tag.P you can return
BRView instead of ParagraphView...

I think it will help you.

Best regards,
valeri
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6276382
The proposed code was snip of code of my previous project. I just cut lines that don't meet your
requirements, but I omited to cut the following in create method :
if(kind == HTML.Tag.P){
         return new ParagraphView(elem)/*javax.swing.text.ParagraphView(elem)*/;

Sorry about it!!!

It is done by default. In this line I returned my own class that meets my own requirements.
But if you want to replace HTML.Tag.P with HTML.Tag.BR return BRView(elem) instead of ParagraphView(elem).(In this case these lines already have matter :-))) )
Did you try this?
Good luck....

Best Regards,
Valeri Kronin
0
 
LVL 2

Author Comment

by:xxMariusxx
ID: 6277836
Valeri,

Yeah, I figured that part out...I went and wrote my own BRView inner class that extends InlineView and returned that instead.

Since your code was returning ...text.ParagraphView rather than ...text.html.ParagraphView, it initially appeared to fix my problem (your comment code keyed me in on this)...i'm all fixed now though.

Sorry I took my time w/this...I hate to copy-n-paste other people's code without taking the time to go through and understand everything that's going on (couple this w/all the other code I hafta write during the day...little time to play around w/new stuff)...but it was all very helpful.  Thanks again!

Marius
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I remove an object from a 3 40
Problem to Alipay 10 44
Tagging and Merging on Branch 1 30
Java Eclipse Loop 3 21
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question