gpt4 book ai didi

java - 必须自动突出显示从文本字段到文本区域的特定字符串

转载 作者:行者123 更新时间:2023-12-01 16:43:06 25 4
gpt4 key购买 nike

我现在想知道如何自动突出显示文本区域中的文本。我进一步研究并找到了一种方法来做到这一点,但是我不确定它如何在用户输入搜索后仍然可以自动突出显示字符串。

enter image description here

注意:忽略单选按钮

这是我在单击搜索按钮时在操作事件中尝试的方法

 @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();
}

}

预期的输出应该是突出显示“大家好”中的“hello”,但我仍然不知道如何做到这一点。

编辑:

代码现在看起来像这样,但遗憾的是亮点仍然不会出现。

@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));
}
});
}

最佳答案

只需监听 TextFieldtext 属性即可实现此目的。请注意,您只能使用 TextArea 选择单个范围。如果您需要突出显示多个出现的情况,您可能需要使用 TextFlow 及其 rangeShape 方法,如 @Slaw 所建议。

@Override
public void start(Stage stage) throws IOException {
TextArea textArea = new TextArea();
VBox.setVgrow(textArea, Priority.ALWAYS);
Random random = new Random();
for (int l = 0; l < 10; l++) {
for (int i = 0; i < 300; i++) {
textArea.appendText(Character.toString('a' + random.nextInt('z'- 'a' + 1)));
}
textArea.appendText("\n");
}

TextField textField = new TextField();
textField.textProperty().addListener((o, oldValue, newValue) -> {
if (newValue.isEmpty()) {
// no highlighting for the empty search string
textArea.deselect();
} else {
int index = textArea.getText().indexOf(newValue);
if (index < 0) {
// text not found
textArea.deselect();
} else {
// select first occurance
textArea.selectRange(index, index + newValue.length());
}
}
});

Scene scene = new Scene(new VBox(textArea, textField));

stage.setScene(scene);
stage.show();
}

使用 fxml 注册监听器的正确位置是 initialize 方法。

关于java - 必须自动突出显示从文本字段到文本区域的特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com