Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to make text unmodifiable in a specific range? #1260

Closed
PavelTurk opened this issue Dec 10, 2024 · 4 comments
Closed

Is it possible to make text unmodifiable in a specific range? #1260

PavelTurk opened this issue Dec 10, 2024 · 4 comments

Comments

@PavelTurk
Copy link
Contributor

I have TextArea with text which length is, for example, 120 characters. And I need to lock text from the beginning to position 100. I mean that the user should not be able to do anything with the text in the range 0-100.

In my situation it is very difficult to work with textArea.addEventFilter(KeyEvent.KEY_PRESSED,...) because there are menu item accelerators that can be configured via settings. So, I thought, maybe it is possible on text area low level API somehow to catch text change events and block them.

Could anyone help me?

@Jugen
Copy link
Collaborator

Jugen commented Dec 11, 2024

Try overriding the replaceText( start, end, text ) method and then check that start > 100 before calling super.replaceText( start, end, text ).

@PavelTurk
Copy link
Contributor Author

@Jugen Thank you very much. I tested this solution and I think I will use it.

However, I noticed an interesting thing. When you try to paste any text NOT FROM this text area(for example, from you IDE), using Ctrl+V or Shift+Ins then you can block this text modification in replaceText. However, if you copy text FROM this text area and paste it, using same keys, it is inserted without any problems and replaceText is not called.

@Jugen
Copy link
Collaborator

Jugen commented Dec 12, 2024

Okay, then override replace( start, end, doc ) instead, with the same logic as before.

@PavelTurk
Copy link
Contributor Author

@Jugen Yes, it helped. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants