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
addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ce) {
JTextComponent comp = (JTextComponent)ce.getSource();
int pos = comp.getCaretPosition();
Element root = comp.getDocument().getDefaultRootElement();
lineNum = root.getElementIndex(pos);
Element line = root.getElement(lineNum);
try {
String text = comp.getDocument().getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
CodeDoc document = (CodeDoc) other.getDocument();
text = ((CodeView) comp).hasAssm ? CodeView.fromAssm(text) : CodeView.toAssm(text);
document.replace(document.getDefaultRootElement().getElement(lineNum).getStartOffset(), document.getDefaultRootElement().getElement(lineNum).getEndOffset(), text, null);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
});
addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ce) {
JTextComponent comp = (JTextComponent)ce.getSource();
int pos = comp.getCaretPosition();
Element root = comp.getDocument().getDefaultRootElement();
lineNum = root.getElementIndex(pos);
Element line = root.getElement(lineNum);
try {
String text = comp.getDocument().getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
CodeDoc document = (CodeDoc) other.getDocument();
text = ((CodeView) comp).hasAssm ? CodeView.fromAssm(text) : CodeView.toAssm(text);
document.replace(document.getDefaultRootElement().getElement(lineNum).getStartOffset(), document.getDefaultRootElement().getElement(lineNum).getEndOffset(), text, null);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
});
30 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @blockgoblin31! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 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.
blockgoblin31
blockgoblin31OP5mo ago
Bump
JavaBot
JavaBot5mo ago
💤 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.
blockgoblin31
blockgoblin31OP5mo ago
Bump
dan1st
dan1st5mo ago
So you want to copy from a specific part of the document to another? Do you want to overwrite the entire line?
blockgoblin31
blockgoblin31OP5mo ago
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
dan1st
dan1st5mo ago
Can you show the errors? Do both documents always have the same number of elements before copying?
blockgoblin31
blockgoblin31OP5mo ago
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
dan1st
dan1st5mo ago
Does it happen in document.replace? Which library are you using?
blockgoblin31
blockgoblin31OP5mo ago
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
dan1st
dan1st5mo ago
Can you show the stack trace?
dan1st
dan1st5mo ago
What is CodeDoc, specifically line 102?
blockgoblin31
blockgoblin31OP5mo ago
CodeDoc is also in CodeView.java I really need to clean that part up thats just calling PlainDocument.remove
dan1st
dan1st5mo ago
Can you use a debugger and find out what offs and len is where it throws the exception?
JavaBot
JavaBot5mo ago
💤 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.
blockgoblin31
blockgoblin31OP5mo ago
with both TextAreas blank it's running with offs 0 and len 1(Checking the elements theres one element containing a "\n")
dan1st
dan1st5mo ago
which have a \n? and do you get an exception in that case?
blockgoblin31
blockgoblin31OP5mo ago
The document being replaced is just a new PlainDocument, but looking at the elements it has this. And yes I still get the exception.
No description
blockgoblin31
blockgoblin31OP5mo ago
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
dan1st
dan1st5mo ago
what exactly is str here? "\n"?
blockgoblin31
blockgoblin31OP5mo ago
I think it should be blank, I can double check quickly after I finish testing this yes, it is "\n"
dan1st
dan1st5mo ago
and the error happens in that exact situation where offs is 0, len is 1 and str is "\n"?
blockgoblin31
blockgoblin31OP5mo ago
Sorry I missed your message, I just solved the bad location exception problem, now I'm getting this stacktrace
dan1st
dan1st5mo ago
Does it still happen with offs being 0?
blockgoblin31
blockgoblin31OP5mo ago
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 null
dan1st
dan1st5mo ago
What about that: In this case, just add it at the last line?
blockgoblin31
blockgoblin31OP5mo ago
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
JavaBot
JavaBot5mo ago
💤 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.
JavaBot
JavaBot5mo ago
Post Closed
This post has been closed by <@501514065068294154>.
Want results from more Discord servers?
Add your server