JProgressBar - java

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.

Related

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.

Notify Swing Worker thread

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.

How to handle progress dialog correctly in android?

I am new in android and java. I am in a trouble in implementing progress dialog correctly.
I have a code like this
ProgressDialog dialog= new ProgressDialog(Main.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
MY METHOD WHICH GRABS DATA FROM INTERNET;
dialog.dissmiss();
but by implementing this, my method runs well but no progress dialog is visible, again when i comment out the dismiss method, the dialog doesnt stop and i had to force close the app,then how to use this dialog? in aditional dont want to bring any thread here, is there any way to use dialog without any thread? Thanks
Problem here is that any long running tasks such as fetching data from the Internet must be run inside a separate thread, otherwise you're likely to get an ANR if your code runs for more then 5 seconds. The ideal solution in my opinion is to implement an AsyncTask: it lets you run tasks in a separate thread and helps you easily update your UI thread, showing ProgressDialogs or ProgressBars to let your users know that your app is currently busy. Simply place all your ProgressDialog initialization code inside the onPreExecute() method of the AsyncTask, and the dialog.dismiss() call to onPostExecute(). Hope this helps.
Yes, you no need to use Thread class. You can use AsyncTask instead. Start the progress dialog when you call the AsyncTask, dismiss it in the postExecute method.
I personally prefer AsyncTask for progress dialog.
In your onPreExecute, create your above code. In doInBackground, do your background tasks and in onPostExecute display an alertdialog saying work done !

Update JProgressBar Without Knowing Progress

I want to use a JProgressBar but I don't have any measurement of progress for how long the task will take to complete. The idea is to have a progress bar displaying the status of a shutdown process but I don't know how long it takes, and I have no way of editing the class that does the shutdown process.
Is there a way to use a JProgressBar without having any indication of the progress?
Call setIndeterminate(true).
From the javadocs:
To indicate that a task of unknown length is executing, you can put a
progress bar into indeterminate mode. While the bar is in
indeterminate mode, it animates constantly to show that work is
occurring. As soon as you can determine the task's length and amount
of progress, you should update the progress bar's value and switch it
back to determinate mode.
You can have a JProgressBar which displays in indeterminate mode, as described here. The default animation is a 'bouncer,' that is, the progress indicator will go back and forth between the two ends of the bar until you stop it.

Categories