Question: how to limit the number of input characters to the visible area?
See original GitHub issueHello, I am new to RichTextFX. I would like to restrict user input to the visible area of the InlineCssTextArea either by setting the number of maximum characters or rows in the textarea. The visible area of my InlineCssTextArea is only two rows but RichTextFX will let the user input a wall of text. Here is my code:
public void start(Stage primaryStage) {
HBox layout = new HBox();
InlineCssTextArea textArea = new InlineCssTextArea();
textArea.setPrefWidth(225);
textArea.setPrefHeight(35);
textArea.setWrapText(true);
textArea.setBackground(new Background(new BackgroundFill(Paint.valueOf("#004c34"), CornerRadii.EMPTY, Insets.EMPTY)));
String description = "Marked task Buy Milk complete";
textArea.appendText(description);
textArea.setStyle(0, "Marked task ".length(), "-fx-fill: #ffffff;");
textArea.setStyle(description.indexOf("Marked task ") + "Marked task ".length(), description.lastIndexOf("complete"), "-fx-font-weight: bold; -fx-fill: #008000;");
textArea.setStyle(description.lastIndexOf("complete"), description.length(), "-fx-fill: #ffffff;");
layout.getChildren().addAll(textArea);
Scene scene = new Scene(layout);
primaryStage.setScene(scene);
primaryStage.show();
}
I combed through the RichTextFX API and searched on Google. I found issue #763 which I was able to use.
Adding the following code gets me what I want but I don’t have any undo history. If I don’t clear the undo history, then previously deleted characters might get undone. If the forgetHistory line is moved into the IF-statement above it, then there appears to be a race condition between textArea.undo() and forget undo history. For example, given a string of 30 characters. Delete 5 characters using the backspace or delete key. Immediately press an alphanumeric key and the deleted characters become undone.
InputMap<Event> map = InputMap.consume(anyOf(keyPressed(ENTER, SHORTCUT_ANY, SHIFT_ANY)));
Nodes.addInputMap(textArea, map);
textArea.setOnKeyReleased(event -> {
if (textArea.getLength() > 25) {
if ( (!event.getCode().equals(KeyCode.BACK_SPACE)) &&
(!event.getCode().equals(KeyCode.DELETE)) ) {
textArea.undo();
}
}
textArea.getUndoManager().forgetHistory();
});
Does this make sense? Or am I missing something? Are there any plans to support setting a limit on the text length? Thanks for your time.
Issue Analytics
- State:
- Created 4 years ago
- Comments:17
Top GitHub Comments
To align the text in a textarea to the right you need to use
setParagraphStyle
instead of setStyle, so try:textArea.setParagraphStyle( 0, "-fx-text-alignment: right;" );
I’m closing this issue since my original question was answered. Thanks for going above and beyond my expectations.