Solved

HTML and JEditorPane

Posted on 2001-07-07
8
650 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
firstChar challenge 13 85
wordappend challenge 8 85
Java passing arguments (type error) 2 35
maven project error 5 24
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 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

23 Experts available now in Live!

Get 1:1 Help Now