Solved

HTML and JEditorPane

Posted on 2001-07-07
8
651 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 81
mapAB Challlenge 35 128
Java / Linux and Regular Expressions 11 71
How to monitor the number of connections/threads being utilized on Tomcat 6.0 8 56
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now