I am trying to create a TextArea using java that contains text that cannot be deleted/updated. That being said, the user can still add to the TextArea and delete their own text.
I've tried to play around with getting the length of the TextArea and listening for the 'backspace' keypress however I can still select all the text in the TextArea and replace it.
Example Code (TA is the TextArea)
TA.setText(cmdx);
cmdLength = TA.getDocument().getLength();
TA.getKeymap().addActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
TA.disable();
try {
backSpace();
} catch (BadLocationException ex) {
Logger.getLogger(CmdFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
AND
public void backSpace() throws BadLocationException {
System.out.println("Backspace is pressed");
int currentLength = TA.getDocument().getLength();
if(currentLength > cmdLength)
{
Document doc = TA.getDocument();
doc.remove(doc.getLength() - 1, 1);
TA.enable();
}
}
I apologize if this question has already been asked.
Thank you in Advance!