I\'ve added a keylistener to my JTextArea field, but it doesn\'t behave as I expected.
inputTextArea.addKeyListener(new KeyAdapter() {
public void keyPressed(Key
Instead of doing the actions immediately on receiving the event, sequence them for later by posting them using SwingUtilities.invokeLater(). The code should look like:
if(k.isShiftDown()) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
inputTextArea.append(" \n");
}
});
} else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//rest of the else body here
}
});
}
In my opinion, the problems seen here are because application-defined actions and internal actions are not being properly sequenced, leading to repaints happening before the text has been modified.