question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: how to limit the number of input characters to the visible area?

See original GitHub issue

Hello, 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:closed
  • Created 4 years ago
  • Comments:17

github_iconTop GitHub Comments

1reaction
Jugencommented, Aug 5, 2019

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;" );

0reactions
HoneyWTFBBQcommented, Aug 13, 2019

I’m closing this issue since my original question was answered. Thanks for going above and beyond my expectations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to specify minimum & maximum number of characters ...
To set the maximum character limit in input field, we use <input> maxlength attribute. This attribute is used to specify the maximum number...
Read more >
Limit number of characters allowed in form input text field
The maximum number of characters that will be accepted as input. This can be greater that specified by SIZE , in which case...
Read more >
Is it possible to limit the input field to a visible number of ...
We have a requirement in which we have a input field which have blank value as of now. But the visible character size...
Read more >
How to limit the number of characters entered in a textarea in ...
In an HTML form, a textarea element specifies a text input area. The author can, and indeed must, suggest the visible size of...
Read more >
HTML attribute: maxlength - MDN Web Docs
The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found