How to add blank lines to the end of a Document?
I'm working on a text editor and it needs to copy some text from one JTextArea to another at the same line, but when one adds a line I can't copy it over with the code I'm using because the document doesnt have enough Elements. My code is
30 Replies
⌛
This post has been reserved for your question.
Hey @blockgoblin31! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
Bump
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
Bump
So you want to copy from a specific part of the document to another?
Do you want to overwrite the entire line?
yes, they should effectively be mirroring each other constantly, its effectively two different formats of the same text
I have copying working except on the bottom line of the document, there it doesnt work and spams errors
Can you show the errors?
Do both documents always have the same number of elements before copying?
No, that’s what I’m trying to fix, I don’t know how to add a new element of a blank line if the element doesn’t exist. It’s a BadLocationException
Does it happen in
document.replace
?
Which library are you using?Yes, when that calls Document.remove
It’s a custom class that I didnt include in the post for… some reason
I’ll get that now
Can you show the stack trace?
What is
CodeDoc
, specifically line 102?CodeDoc is also in CodeView.java
I really need to clean that part up
thats just calling PlainDocument.remove
Can you use a debugger and find out what
offs
and len
is where it throws the exception?💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
with both TextAreas blank it's running with offs 0 and len 1(Checking the elements theres one element containing a "\n")
which have a \n?
and do you get an exception in that case?
The document being replaced is just a new PlainDocument, but looking at the elements it has this. And yes I still get the exception.
I wonder if its an off by one from .getEndOffset()?
and yes it was
now the only problem is if they have different numbers of elements
what exactly is
str
here? "\n"
?I think it should be blank, I can double check quickly after I finish testing this
yes, it is "\n"
and the error happens in that exact situation where offs is 0, len is 1 and str is
"\n"
?Sorry I missed your message, I just solved the bad location exception problem, now I'm getting this stacktrace
Does it still happen with offs being 0?
no, that only happens if the Document I'm currently editing has more lines than the other document and I have one of those lines selected, it happens before the .replace call
its the
document.getDefaultRootElement().getElement(lineNum)
call returning nullWhat about that: In this case, just add it at the last line?
And moving the index back by one and putting \n at the start of the text if the line doesnt exist worked
I tried that earlier but used the wrong length
ty
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
Post Closed
This post has been closed by <@501514065068294154>.