Author Topic: [Java] Modifying a JTextArea causes the field to 'select all'  (Read 2082 times)

0 Members and 1 Guest are viewing this topic.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
[Java] Modifying a JTextArea causes the field to 'select all'
« on: December 27, 2007, 02:15:04 pm »
I've got a tab-completion system where, when the user presses tab, a list pops up in a dialog below the chat window with all of the users that match the search pattern. When the user selects a user from the list, the contents of the box change to "[username]: {cursor}" if there are no spaces prefixing the tab completion string, or to "[prefix] [username] {cursor}[suffix]" otherwise.

Code: [Select]
private void tcSelect(String selection) {
if((tcBefore.length() == 0) && (tcAfter.length() == 0)) {
chatTextArea.setText(selection + ": ");
chatTextArea.setCaretPosition(chatTextArea.getText().length());
} else {
chatTextArea.setText(tcBefore + selection + tcAfter);
chatTextArea.setCaretPosition(tcBefore.length() + selection.length());
}

// Exit TC mode
tcPopupWindow.setVisible(tabComplete = false);
}

When using certain look and feels, this results in the entire entire text area being selected. The affected Look and Feels vary based on OS, but Windows seems to be affected by most of them.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!