user11611653
user11611653

Reputation: 139

How to prevent a lightweight component receive mouse events?

I have some JTextComponents with some CaretEvents. I need to disable all the components to prevent user interaction after a certain actions and when that actions are completed reenable those JTextComponents to allow the user to interact again with the GUI. Obviously when the user clicks on a JTextComponent the caret listener performs it's actions and I want to prevent that.

I currently removed the CaretListeners to prevent that and after my internal operations are completed I re-add those CaretListeners.

Is there any other way to prevent that? Such removing the MouseEvents for those components and reassign that later?

Upvotes: 1

Views: 170

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285401

One way to disable user interaction is to not allow the text component to receive the program's focus:

myTextComponent.setFocusable(false);

And then later when you want to re-allow interaction, make the same call with the obvious true parameter:

myTextComponent.setFocusable(true);

Upvotes: 1

Related Questions