Highlighting Strings in Javafx Textarea

Highlighting Strings in JavaFX TextArea

The JavaFX TextArea control (as of 2.0.2) does not support rich text editing where text styles (fonts, etc) are mixed.

You can highlight contiguous strings of characters in the TextArea by manipulating the TextArea's selectRange, as in the following example:

public class TextHighlight extends Application {
public static void main(String[] args) { Application.launch(args); }
@Override public void start(Stage stage) {
final TextArea text = new TextArea("Here is some textz to highlight");
text.setStyle("-fx-highlight-fill: lightgray; -fx-highlight-text-fill: firebrick; -fx-font-size: 20px;");
text.setEditable(false);
text.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent t) { t.consume(); }
});

stage.setScene(new Scene(text));
stage.show();

Platform.runLater(new Runnable() {
@Override public void run() { text.selectRange(13, 18); }
});
}
}

You could use the above code as a basis to switch the TextArea to read-only mode while spell checking is happening. Implement prompting to find and fix each word in turn until the spell check is complete. Perform the prompting in a separate dialog or panel. The Jazzy demo seems to work this way http://jazzy.sourceforge.net/demo.html, so it should be fairly easy to convert its Swing UI to JavaFX.


Alternately, you could use a JavaFX WebView control to wrap any of the many javascript/html based spell checkers (e.g. http://www.javascriptspellcheck.com/) using a technique similar to what is demonstrated here: http://jewelsea.wordpress.com/2011/12/11/codemirror-based-code-editor-for-javafx/.

Highlighting strings in JavaFX TextArea

Unfortunately rich text is not yet supported (fx 2.2).

You can try next workarounds:

  1. Use RichTextEditor
  2. Use WebView
  3. Draw several Text on Canvas
  4. Just put Text/Labels with different formatting in HBox or TilePane.
  5. Create a transparent pane over the TextArea and draw an opaque highlights, but this can be hard due to complexity of finding out exact words coordinates.

JavaFX display text area appending - any way to highlight/make new text more visible?

As suggested in Highlighting Strings in JavaFX TextArea, you can select the new text.

Assuming the text area is not editable, and you are only ever appending to the end of the existing text, you can do:

textArea.textProperty().addListener((obs, oldText, newText) -> {
if (newText.length() > oldText.length()) {
Platform.runLater(() ->
textArea.selectRange(oldText.length(), newText.length()));
}
});

If your text area is editable, or you are using it more generally, then you can do something similar each time you append text. For example:

String message = ... ;
textArea.positionCaret(textArea.getLength());
textArea.appendText(message);
textArea.extendSelection(textArea.getLength());

How to know which text string is selected by user in JavaFX TextArea

Use getSelectedText() to get the selected text.

The answer to your second question is yes.

The getSelectedText() method can be used like I have done here:

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextAreaDemo extends Application
{
@Override
public void start(Stage stage)
{
final TextArea textArea = new TextArea("Text Sample");
textArea.setPrefSize(200, 40);

textArea.setOnContextMenuRequested(new EventHandler<Event>()
{
@Override
public void handle(Event arg0)
{
System.out.println("selected text:"
+ textArea.getSelectedText());
}
});

VBox vBox = new VBox();
vBox.getChildren().addAll(textArea);

stage.setScene(new Scene(vBox, 300, 250));
stage.show();
}

public static void main(String[] args)
{
launch(args);
}
}

Once you launch this application, it shows a TextArea with some text (Text Sample). I selected some part of the text and did a right click. It printed the selected text. Does this fit your requirement?



Related Topics



Leave a reply



Submit