Siddhu
Siddhu

Reputation: 1248

how to stop action performed in java swing

I am creating a java desktop application.I have two button "create" and "cancel". if i press create it will create a report in a new window. my problem is on "cancel" button. if the user press the create button it will take some time to create a new report. the cancel button should work only in the creation time. if the user presses cancel button, the report creation action should abort. i add action listener to create and cancel button but when i click create i am not able to click cancel button simultaneously.

Upvotes: 3

Views: 3134

Answers (2)

Perley
Perley

Reputation: 302

Try running each process in a separate thread. The reason cancel won't work is create takes time to finish its process. In that time it hogs the main thread and halts any actions (including the cancel button) that could otherwise be taken by other UI elements.

Your cancel button is probably working. But each time you click on it, the event that gets fired is put into the back of the queue for processing behind the create button's event. By putting the handling of the event in a separate thread and synchronizing any resources needed by both buttons (as long as those resources are thread safe!), you should be able to stop the thread created by the event button easily with the cancel button.

Upvotes: 2

asteri
asteri

Reputation: 11572

Try creating two different ActionListeners: one for the "Create" JButton and one for the "Cancel" JButton. This should allow one to respond while the other is processing.

Upvotes: 0

Related Questions