I am now wondering how to automatically highlight the text within a text area. I have researched further and found a method to do so however I am not sure how it can still highlight a String automatically after the user enters search.
Note: Ignore the radio buttons
Here is the method I tried within the action event when clicking the search button
@FXML
private void searchButton(ActionEvent actionEvent){
String key = keyField.getText();
String field = textField.getText();
System.out.println(key);
System.out.println(field);
textField.getText();
if (textField.getText().equals(key)) {
textField.setStyle("-fx-highlight-fill: yellow; -fx-highlight-text-fill: black; -fx-font-size: 12px; ");
textField.setEditable(false);
textField.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
t.consume();
}
});
Platform.runLater(new Runnable() {
@Override
public void run() {
textField.selectRange(13, 18);
}
});
}
ToggleGroup group = new ToggleGroup();
rabinKarpp.setToggleGroup(group);
kmp.setToggleGroup(group);
naive.setToggleGroup(group);
ArrayList indexes = new ArrayList();
if (rabinKarpp.isSelected()){
indexes = RabinKarp.process(field, key);
}
else if (kmp.isSelected()){
indexes = KMPAlgo.process(field, key);
}
else if (naive.isSelected()){
indexes = NaiveAlgo.process(field, key);
}
else if(!naive.isSelected() && !kmp.isSelected() && !rabinKarpp.isSelected()) {
Alert alert = new Alert(Alert.AlertType.WARNING, "Select an algorithm first before proceeding.",ButtonType.OK);
alert.showAndWait();
}
}
the expected output should be the "hello" from "hello everyone" to be highlighted but I'm still stumped on how to do so.
EDIT:
The code now looks like this but still the highlights will not turn up sadly.
@FXML protected void searchButton(ActionEvent actionEvent){
String key = keyField.getText();
String field = textField.getText();
System.out.println(key);
System.out.println(field);
ToggleGroup group = new ToggleGroup();
rabinKarpp.setToggleGroup(group);
kmp.setToggleGroup(group);
naive.setToggleGroup(group);
ArrayList indexes = new ArrayList();
if (rabinKarpp.isSelected()){
indexes = RabinKarp.process(field, key);
}
else if (kmp.isSelected()){
indexes = KMPAlgo.process(field, key);
}
else if (naive.isSelected()){
indexes = NaiveAlgo.process(field, key);
}
else if(!naive.isSelected() && !kmp.isSelected() && !rabinKarpp.isSelected()) {
Alert alert = new Alert(Alert.AlertType.WARNING, "Select an algorithm first before proceeding.",ButtonType.OK);
alert.showAndWait();
}
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
List<List<Integer>> locations = new ArrayList();
keyField.textProperty().addListener((o, oldValue, newValue) -> {
Pattern pattern = Pattern.compile(newValue);
Matcher matcher = pattern.matcher(textField.getText());
locations.clear();
if (newValue.isEmpty()) {
// no highlighting for the empty search string
textField.deselect();
} else {
int index = textField.getText().indexOf(newValue);
if (index < 0) {
// text not found
textField.deselect();
} else {
while(matcher.find())
{
List<Integer> tempList = new ArrayList<>();
tempList.add(matcher.start());
tempList.add(matcher.end());
locations.add(tempList);
}
}
}
});
AtomicInteger currentIndex = new AtomicInteger(-1);
downButton.setOnAction((t) -> {
if(currentIndex.get() >= -1 && currentIndex.get() < locations.size() - 1)
{
textField.selectRange(locations.get(currentIndex.incrementAndGet()).get(0), locations.get(currentIndex.get()).get(1));
}
});
upButton.setOnAction((t) -> {
if(currentIndex.get() > 0 && currentIndex.get() <= locations.size())
{
textField.selectRange(locations.get(currentIndex.decrementAndGet()).get(0), locations.get(currentIndex.get()).get(1));
}
});
}