1

I wrote a text editor that saves the text as html.

I'm having no problem with the styles like bold, italic, etc etc, the only problem I'm having is the way it behaves when I press enter. Instead of creating a new normal line, it creates a extra spaced line. I think it has something to do with the <p> tag but I'm not sure... Anyway here's an example of my problem:

import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.html.HTMLEditorKit;

public class test extends JFrame {

    public static void main(String[] args){
        new test().open();
    }

    private void open() {
        setSize(200, 200);
        JEditorPane jp = new JEditorPane();
        setLayout(new BorderLayout());
        add(jp, BorderLayout.CENTER);

        jp.setEditorKit(new HTMLEditorKit());
        jp.setText("<html><body><p>hey</p><p>Write in here</p></body></html>");
        setVisible(true);
    }
}

Is there anyway to fix this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Luso
  • 125
  • 2
  • 9

1 Answers1

0

I think you are trying to edit directly onto the JEditorPane after it renders the HTML. JEditorPane reads in the text you provide it and renders it in accordance with the HTML markup that accompanies the text. So, if you want to make it have a newline at some point in your text, then you need to input a break tag, like so:

public class test extends JFrame
{
  public static void main(String[] args){
    test t = new test();
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    t.open();
  }

  private void open()
  {
    setSize(200, 200);
    JEditorPane jp = new JEditorPane();
    jp.setEditable(false);
    setLayout(new BorderLayout());
    add(jp, BorderLayout.CENTER);

    jp.setEditorKit(new HTMLEditorKit());
    jp.setText("<html><body><p>hey</p><p>Don't write here.<br>Let JEditor rendor your HTML text. </p></body></html>");
    setVisible(true);
  }
}

Hope this helps.

Alan
  • 822
  • 1
  • 16
  • 39
  • Thank you for your answer but that doesn't actually answers the question... The JEditorPane is intended to let users edit the text of an HTML file (per example) not only view it. – Luso Jan 18 '14 at 18:46