Solved

HTML and JEditorPane

Posted on 2001-07-07
8
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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

Industry Leaders: 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 …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

632 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