0

I want to remove the first line in jtextpane and setting setContentType("text/html") at the same time.

setContentType("text/html");

Element root = getDocument().getDefaultRootElement();
Element firstLine = root.getElement(0);
getDocument().remove(firstLine.getStartOffset(), firstLine.getEndOffset());

When I use setContentType("text/html") in the code it does not remove the first line.

4
Contributors
10
Replies
24
Views
2 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • How many times have we said this in the Java forum... Never NEVER *NEVER* do this when writing new code: } catch (Exception e) { } If/when there is an error you just told Java that you didn't want to know anything about it, and please discard the detailed error … Read More

0

I don't know, but you could try printing the contents of firstLine to see what's happening. Maybe it's something that's not displayed, like "<html>" ???

0

I get an exception.

try {
    Element root = getDocument().getDefaultRootElement();
    Element firstLine = root.getElement(0);
    getDocument().remove(firstLine.getStartOffset(),    firstLine.getEndOffset());
   }
catch (Exception e) {}

when using setContentType("text/html");

2

How many times have we said this in the Java forum...
Never NEVER NEVER do this when writing new code:

} catch (Exception e) {
}

If/when there is an error you just told Java that you didn't want to know anything about it, and please discard the detailed error message that Java just created for you.
ALWAYS put an e.printStackTrace(); in your catch blocks until/unless you have a good reason to do something else.

0

This did not solve the problem I am having. I just forget to write something in catch block.

Can anybody help? I would appreciated.

0

This is the exception I got

javax.swing.text.BadLocationException: Invalid remove

The code works fine if I commented setContentType("text/html");

0

Despite your unwillingness to supply all the relevant info, I'll give you this as a present:
the remove method does NOT take two offsets as parameters. Check the api doc for the correct parameters. YOu will see that this bug is going to be able to bite you regardless of setting a doc type.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.