Created
December 26, 2024 21:24
-
-
Save sandipchitale/b09d437f1e12d715adb5dde613150461 to your computer and use it in GitHub Desktop.
Search in text area #search-in-java-swing-text-area
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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