突出显示JavaFx TextArea中的文本 [英] Highlight text in JavaFx TextArea
问题描述
我有一个如下所示的文本区域,
I have a text area like below,
我需要突出显示或选择所有文本突出显示我".我找不到任何方法来突出显示文本区域中的文本.另外,我在JavaFX中找不到其他任何API来突出显示Swing中特定文本或字母(如JTextArea)的出现.有人可以建议我如何在文本区域中突出显示String吗?还是JavaFX中除此文本区域外还有其他可用的API?
I need to highlight or select all the text "Highlight me". I don't find any methods to highlight the text in text area. Also I could not find any other API in JavaFX that highlights the occurrence of specific text or letter like JTextArea in Swing. Can any one suggest me on how to highlight String in the text area? Or is there any other API available apart from this text area in JavaFX?
我的代码:
public class FXTextArea extends Application {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(final Stage primaryStage) {
Group root = new Group();
final TextArea textArea = TextAreaBuilder.create().prefWidth(390)
.wrapText(true).build();
ScrollPane scrollPane = new ScrollPane();
scrollPane.getStyleClass().add("noborder-scroll-pane");
scrollPane.setContent(textArea);
scrollPane.setFitToWidth(true);
scrollPane.setPrefWidth(390);
scrollPane.setPrefHeight(180);
Button buttonLoad = new Button("Load");
buttonLoad.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
FileChooser fileChooser = new FileChooser();
// Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
"TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
// Show save file dialog
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
textArea.setText(readFile(file));
}
System.out.println(textArea.getText(0, 30));
textArea.selectRange(0, 30);
}
});
VBox vBox = VBoxBuilder.create().children(buttonLoad, scrollPane)
.build();
root.getChildren().add(vBox);
primaryStage.setScene(new Scene(root, 400, 300));
primaryStage.show();
}
private String readFile(File file) {
StringBuilder stringBuffer = new StringBuilder();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(file));
String text;
while ((text = bufferedReader.readLine()) != null) {
stringBuffer.append(text);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(FXTextArea.class.getName()).log(Level.SEVERE,
null, ex);
} catch (IOException ex) {
Logger.getLogger(FXTextArea.class.getName()).log(Level.SEVERE,
null, ex);
} finally {
try {
bufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(FXTextArea.class.getName()).log(Level.SEVERE,
null, ex);
}
}
return stringBuffer.toString();
}
}
预期输出:
线条应以任何颜色突出显示,如下所示. 突出显示我"的出现应突出显示如下,
The lines should be highlight in any color as below. The "Highlight me" occurrence should be highlighted as below,
推荐答案
You can take help from this. But the constraint is that you need to be on jdk8.
这篇关于突出显示JavaFx TextArea中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!