Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Created December 26, 2024 21:24
Show Gist options
  • Save sandipchitale/b09d437f1e12d715adb5dde613150461 to your computer and use it in GitHub Desktop.
Save sandipchitale/b09d437f1e12d715adb5dde613150461 to your computer and use it in GitHub Desktop.
Search in text area #search-in-java-swing-text-area
private void searchActionMap(String text, JTextArea textArea) {
if (text.isEmpty()) {
textArea.setCaretPosition(0);
} else {
int index = textArea.getText().toLowerCase().indexOf(text.toLowerCase(), textArea.getCaretPosition());
if (index == -1) {
// Try to wrap
index = textArea.getText().toLowerCase().indexOf(text.toLowerCase());
if (index == -1) {
// Not found
Toolkit.getDefaultToolkit().beep();
} else {
textArea.setCaretPosition(index);
textArea.moveCaretPosition(index + text.length());
}
} else {
textArea.setCaretPosition(index);
textArea.moveCaretPosition(index + text.length());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment