Notify Swing Worker thread - java

I am using Swing Worker to show progress dialog and run the background process. I need functionality like when user clicks on cancel button of progress dialog the confirmation dialog is displayed , the process will wait for user input and if user selects yes the process stops. If user selects no the process will continue.
I am able to stop the background process using SwingWorker.cancel function but not able to start the process again if user clicks on no button.

SwingWorker cannot be restarted once cancelled. Similar to basic Threads, they execute once. From the docs
SwingWorker is only designed to be executed once.
You could simply have each button dismiss the cancel dialog with the "No" button taking no additional action on the SwingWorker.

Related

Android best practice to handle UI during a network call

I want to know what is the best way to handle a UI during a network call. For example -
I have a login screen with login button and email and password input fields. I enter email and password fields and press login button. I show a progress dialog. I subscribe now with RxJava and I have my observer in presenter. He tells the view to show a progress dialog. The API call is in progress. Stil my observable has not returned anything back. Now I press back button and my progress dialog is dismissed. I again press login button and a second call is done. So I can do this repeatedly and queue my requests.
What is the best practice to handle this scenario where user is allowed to make one request per once button click? I know there are several ways to do it in view such as greying out the button or using a progress bar which is non dismissable. But I am more interested in knowing how to deal this scenario in my model layer. I want my view to be as dummy as possible.
Why not override onBackPressed to cancel any in-progress threads?

Disable SWT Dialog close programmatically

I'm working on a SWT application in which I'm opening a dialog window. In the dialog window after the user clicks on a button it will execute a task on a different thread. This task will take around 1-2 minutes to complete. After the task is completed, the dialog is updated with the results. I want to prevent the user from closing the dialog box before the task is completed otherwise the task thread tries to update the dialog and swt disposed exception is thrown.
Can anyone tell me how to override the close operation programmatically. I want to avoid creating a custom Dialog class by extending the SWT Dialog class

How to stop mouse click when clicking on a dialog using java swt

I have a dialog and when the user clicks the ok button in the dialog, the call goes from client to the server and then starts processing. In mean time when it is in the processing stage when the user tries to click anywhere on the dialog it is getting hanged and then once the process gets complete it behaves normally. So until the process gets complete i dont want the user to click the dialog, even though if he clicks the event should not be detected and dialog should not get hanged.
I dont want use progress monitor, is there anyway to handle this?
This is code I am using after OK button Pressed
`//Server call
startServerProcess(compsTable);
//Async to update UI
Display.getDefault().asyncExec( new Runnable()
{
public void run()
{
label.setText("");
}
});`
Even though the async call is used, when user clicks anywhere on the dialog it shows hanged and says not responding. Any help for this? –
Unless you are doing it asynchronously, it will behave like it does. the SWT is waiting until it gets the response back from the server, and during that time, whatever you do (e.g. click or do other actions) will not have any affect because it is not ready for user interaction yet.
You can run the job in a Thread, but ProgressMonitor was designed to give you a nice modal UI dialog telling you to wait. If you run a separate thread, you'll have to check if they click on the OK button twice, or some other element you left accessible.
I my opinion ergonaut's answer is correct and You should go with threads and asynchronous processing.
But if You insist to do it in one UI(!) thread then disable dialog parent composite, send, receive and process server's response. Then enable parent composite. This will block unnecessary events during processing.
parent.setEnable(false);
send(message)
process(recv());
parent.setEnable(true);
Be aware that user expects some kind of notification when something is processing. Without showing that app is busy user probably assume that application hangs and terminate it.

how do I use wait notify to stop pause a method while waiting on a button to be pressed in another class

So using a GUI in java, I am trying to allow the user to press a transfer button which will initiate a method. This method will open up a separate GUI which will ask the user to enter information and then save it when the user has pressed a save button. What I want to do is pause the method after it has opened the separate GUI and continue once the user has hit the save button in that second GUI. Unless there is a better way, I believe that implementing a wait notify method is the best way to go about this but I have been unsuccessful thus far.
Firstly, DON'T (use wait/notify) for any reason within the Event Dispatching Thread. This will cause the UI to stop responding to input events and repaint request, effectively "hanging" your program.
In your case, it would be a simple case of using a modal dialog.
Check out How to make dialogs and Concurrency in Swing

JProgressBar

any methods available to stop a JProgressBar at runtime?? i wil start a progress bar wen a button is clicked(which wil start a thread at the back ground). what i exactly want is to stop the progress bar wen a deadlock situation occurs in my program.. This is the gist of my program..
Perhaps you can use ProgressMonitor?
From the API documentation:
Deciding Whether to Use a Progress Bar or a Progress Monitor
Use a progress monitor if:
You want an easy way to display progress in a dialog.
The running task is secondary and the user might not be interested in the progress of the task. Progress monitor provides a way for the user to dismiss the dialog while the task is still running.
You want an easy way for the task to be cancelled. Progress monitor provides a GUI for the user to cancel the task. All you have to do is call progress monitor's isCanceled method to find out if the user pressed the Cancel button.
Just some thoughts, hope this helps.

Categories