Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HTML and JEditorPane

Posted on 2001-07-07
8
Medium Priority
?
681 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 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

963 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