Java add progressbar for current Thread - java

How can I add a progressbar that would show how much time will it take untill the current process in done?
I do know how to construct and use a progressbar, in simple ways, but I do not know how to bound them with a process.
For example when I click a button, I would like to open a jfilechooser, and this may take some time. I want the user to know that the app did not crash and to give an idea of how much time will it take until the jfilechooser appears.
Thanks in advance

The thing is, when dealing with classes you didn't write, it's not that easy to link the progress bar with the actual progress.
If you'd like to bind it with a progress of some task, here are the steps:
Assign a thread to the time-consuming task
Use a static field in the parent class as the progress you've accomplished in your task. (may be counter from 1 to 100)
Every specific period (1 sec) you check that static field and set the progress bar with it.
Inside your task, you divide your long process into solid steps (i.e. opening a file, processing, preparing extracted data ...etc).
After every solid step, update the static field in the parent class with the progress so far.
Make sure to regulate the process of writing and reading the static field using semaphores.

Related

java: how to delay code in GUI

I am making a programme in Java for a school project. It has the form of an interactive multiple-choice test.
I wanted it to perform it in a way that an action button creates an event in which (the order of the code is the same as listed):
1) Randomly chooses an Object that consist of several Strings from a prepared List and prints it on adequate text fields in GUI
2) Using a proper method there is a delay created that holds further code down below for 1 minute. In this time the user should be able to check proper Checkboxes so the GUI must stay active.
3)When this minute ends the checked places are read and further processed.
The thing is that I am unable to create step number 2) which is the delaying of the code below. I have tried function sleep() but when I do it with sleep() the whole GUI freezes and the user is unable to do anything on it. I have read that function swing timer would be appropriate but I dont know how to do it. I have seen examples but in them the timer along with functions that were executed after some time were written in the class ActionListener instead of the action button. I am using Netbeans 8.1
Sorry for my bad explanation of the problem, I am a total beginner in java programming and really count on your help :)
Cheers!
Your problem comes from how you structured your code. You wrote everything into a single method.
Split this up into two methods. One to set up the UI state (everything before "HERE I NEED TO DELAY..."). The second method takes everything below.
Then, at the end of the first method, create a non-repeating Timer for one Minute, add an ActionListener that just calls your second method. Then start the timer. When the timer has run its course, it will call your second method through the action listener.

How to run progress bar while executing the mysql query in java?

I want to delete duplicate record in my database table, and I do it in java by using this query
String sql = "DELETE e1 FROM tweet_after_preprocessing e1, tweet_after_preprocessing e2 WHERE e1.tweet = e2.tweet AND e1.tweet_after_preprocessing_id > e2.tweet_after_preprocessing_id"
The problem is when there are so many records in my database table, the process will take so long, and make my program look not curently running.
and I want to use progress bar to show progress of the executing, how can I do that?. I don't now the maximum and the minimum value, so how can i accessing the progress bar?.
You can create an indeterminate progress bar by setting the property indeterminate to true: JProgressBar.html#setIndeterminate().
Also it is wise to not execute long lasting work in the EDT but use a different thread for this.
The problem is that you're doing all your work on the EDT, which is blocking your GUI. You need to do the loading on another Thread, so your GUI can still update, display, and respond to user input.
Once you have the work on another thread, then from that thread, you can post updates to the EDT using the SwingUtilities.invokeLater() method.
You could also look into the SwingWorker class, which handles some of that for you.
Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/
Excellent question.
Jenkins uses the time of the last build to guess how long the current build will take to run. So for example if the last time a build ran it took 10 minutes, and it is 5 minutes into a current build it will show that it is 50% complete.
You can do something similar. Maybe query the DB first to see how many items need to be deleted and have a table of how long it will take for the amount of items being deleted and how many items are in the table.

Latency when pausing/stopping WaveAudioStream

I have just found the code to a small WAV player.
It works well but when clicking the "Pause" and "Stop" buttons there's like a 2 seconds delay which makes the app look really unprofessional. I have no idea what is causing this but I'd really like to have it fixed, could anyone inspect the code and tell me where it comes from? Thanks!
I wrote this sample for a time and don't remember very well.
In my opinion the latency comes mainly from the update frame functions. In the class VisualPlayer, UI are update by a timer which pick up the current values from the thread. It's not very efficient but faster for write this sample.
The best way to update UI is implementing a Runnable class and call it with SwingUtilities.invokeLater().
Also have you try to reduce read buffer size ?

ProgressBar using Threads in SWT

In my SWT Application i wish to use a progress bar (org.eclipse.swt.widgets.ProgressBar)
i found examples but could not understand the proper flow and usage i need to perform background task for which, i was paused at the algorithm phase like i can analyze that there should be two threads one for background job, another for a progressbar both should execute simultaneously, i could not figure out the Implementation of this,i found plenty of examples but,i was puzzled where should i place my background task code (still searching). Can anyone provide sample example that suits me.
Did you see update a progress bar (from another thread) from SWT ProgressBar snippets page? It explains a lot.
ProgressBar have to be asynchronous to work correctly in every GUI framework. It's job is to show some value independently on main (GUI) thread. So you have to write your own thread, which will maintain the ProgressBar, resolving current value of it and show the changes of value (rendering progress by widget itself).
In Swing, you don't have to create you own thread for ProgressBar, because the class already implements upper proposed behavior (makes itself another thread and manage the work for you). But because SWT is platform dependent (and for other reasons), you have to manage this by yourself.

Swing "blocking", I think I need to thread, but not sure how much

I have a little java app to effectively "tail" an arbitrary collection of files defined in an ini file. My "LogReader" class extends JFrame, and does the heavy lifting; reading the collection of file paths into a vector, and then iterating over the vector, reading each file and adding the last X lines of each to a text areas on the tabs of a JTabbedPane. The process of building the vector and iterating over the files is kicked off by clicking a JButton, via an ActionListener.
The reading of the files worked fine (and still does), but the process of reading 20-some files, some growing as large as 30MB, takes some time. To help pass that time, I decided to add a progress screen, which says "Now reading file #3 of 26: c:\logs\superduper1.log", and so on. So I created another class, "SplashScreen", also extending JFrame, and added a JLabel that would indicate the progress. The SplashScreen class has an update() method, which just does a setText() on the JLabel.
The ActionListener on the JButton calls RefreshLogs(), which looks something like:
vctFileStrings.clear();
tpMain.removeAll();
frmSplash.update("Loading Configuration"); //Update the label on the Splash Screen instance
BuildVectorOfLogs(strConfFile); //Read the collection of files into the vector
frmSplash.update("Reading Logs");
ReadLogs(); //read the files, updating the Splash Screen as we go
and then ReadLogs() iterates over the vector, reading the files and building the TabbedPane.
What I noticed, though, is that when RefreshLogs() is called from within the ActionListener, the Splash Screen doesn't update. However, if I add RefreshLogs() to the constructor of the first frame, the splash screen works as expected (updates progress on each file). After some experimenting and reading, I think that I need to create a worker thread that reads the files, while updating the splash screen in the event-dispatch queue.
My questions are:
- Is my thought correct? Is there some simple alternative to implementing threading that would allow me to update the splash screen from the method called by the ActionListener?
- If this would be best accomplished using threading, what scope of the activity would I need to thread? Would I need to put all of the file I/O activities into their own thread? Should I put the GUI activities (label updates) in their own thread, so they occur separately from the JButton click event?
I would say: yes, your thoughts on offloading the reading of large files to a separate thread are correct. You should never perform long tasks on the Event Dispatch Thread, since while that thread is busy, the GUI will be unresponsive, and you application will feel slow.
This sounds like good case for SwingWorker. This class allows you to perform slow requests (such as disk or network access) on a separate thread, with progress updates being fed back to the GUI with the EDT. SwingWorker looks after all complexities of switching between threads. All you have to do is implement your business logic in the appropriate places.
Sun has a tutorial on SwingWorker.
Yes, you should put your time intense reading into a separate thread. Now you do everything in the event-dispatching thread (EDT), which would update your GUI but is busy reading your data.
You can use SwingWorker for this. Have a look at Using a Swing Worker Thread which looks like what you need.
One suggestion for you, to figure out how to do this, and in case you are using NetBeans or have access to NetBeans, is to look at the default Java Desktop Application template. It creates a pre-wired desktop app with progress bar built into a status bar, that will automatically get updated when any "Action" code gets executed. It leverages the Action API which is also pre-wired to run in a background thread.
By looking at that auto-generated code you'll be able to properly and easily implement it in your own.

Categories

Resources