Reputation: 35
I'm trying to create a pop up dialog progress bar preferably an Indeterminate but that's not too important. I have been looking through "Oracle's ProgressBar Tutorials" and Google searching but not such luck in getting it to work. I'm pasted my code below of my Action Listener and the dialog will not pop up. Any suggestions?
Sorry this is my first post on this site. But how it works is that When I press the create button, it goes out and grab some information from different servers and directories and creates a file for me. That is what the new Project is. Features is a Enumeration I made that are set with the text in the JTextBox
when the Create Button. The problem is that this process takes time to process, so I want the a progress bar to show that its processing
private class CreateButton implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0) {
class Task extends SwingWorker<Void, Void>
{
@Override
protected Void doInBackground()
{
//Set Variables
for(Feature f : Feature.values())
{
if(f.getComp() != null)
{
f.getVariable().setVariable(((JTextField) f.getComp()).getText());
}
}
new Project(jobs.getSelectedValue().split("-")[0].trim(),
jobs.getSelectedValue().split("-")[1].trim(),
features);
return null;
}
}
ProgressMonitor pm = new ProgressMonitor(display, "Testing...", "", 0, 100);
pm.setProgress(0);
Task task = new Task();
task.execute();
}
}
Upvotes: 0
Views: 6074
Reputation: 1
You need to define attribute
ProgressMonitor pm;
then should create total progress size
int totalProgress = Feature.values().size();
then in the loop just increment count
int counter = 0;
for(Feature f : Feature.values())
{
if (pm.isCanceled()) {
pm.close();
return null;
}
pm.setProgress(counter);
pm.setNote("Task is " + counter*100/totalProgress + "% completed");
counter++;
call the progress monitor
pm = new ProgressMonitor(display, "Testing...", "", 0, totalProgress);
assumed that the most part of the job is done in the loop, if the other part such as project creation takes time then you could add additional percent counts to totalProgress
or reset monitor after features completed.
Upvotes: 0
Reputation: 8855
I was not sure about your SSCCE so I am just posting how JProgressBar
usually works.
Read about SwingWorker and JProgressBar
During background process show progress bar. A simple example of how it works is shown.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class MyProgressBarTest {
private static final long serialVersionUID = 1L;
private static JProgressBar progressBar;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyProgressBarTest obj = new MyProgressBarTest();
obj.createGUI();
}
});
}
public void createGUI() {
final JFrame frame = new JFrame();
JPanel panel = new JPanel();
final JButton button = new JButton("Progress");
progressBar = new JProgressBar();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
MyCustomProgressBarDialog progressBarObj = new MyCustomProgressBarDialog(progressBar, frame);
progressBarObj.createProgressUI();
MyActionPerformer actionObj = new MyActionPerformer(progressBar, progressBarObj, button);
actionObj.execute();
}
});
panel.add(button);
frame.add(panel);
frame.setTitle("JProgressBar Example");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocationRelativeTo(null);
frame.setSize(200, 300);
frame.setVisible(true);
}
}
class MyActionPerformer extends SwingWorker<String, Object> {
JProgressBar fProgressBar;
MyCustomProgressBarDialog progressDialog;
JButton button;
public MyActionPerformer(JProgressBar progressBar, MyCustomProgressBarDialog progressDialog, JButton button) {
this.fProgressBar = progressBar;
this.fProgressBar.setVisible(true);
this.fProgressBar.setIndeterminate(true);
this.button = button;
this.progressDialog = progressDialog;
this.button.setEnabled(false);
}
protected String doInBackground() throws Exception {
calculateResult();
return "Finished";
}
protected void done() {
fProgressBar.setVisible(false);
this.progressDialog.setVisible(false);
this.button.setEnabled(true);
}
public void calculateResult() {
for (int i = 0; i < 500000; i++) {
System.out.println("Progress Bar: " + i);
}
}
}
class MyCustomProgressBarDialog extends JDialog {
private static final long serialVersionUID = 1L;
private static JProgressBar progressBar;
private JFrame motherFrame;
private JLabel label = new JLabel("loading.. ");
private JButton button;
public MyCustomProgressBarDialog(JProgressBar progressBar, JFrame frame) {
this.progressBar = progressBar;
this.motherFrame = frame;
this.button = button;
}
public void createProgressUI() {
add(label, BorderLayout.NORTH);
add(progressBar, BorderLayout.CENTER);
setSize(50, 40);
setAlwaysOnTop(true);
setLocationRelativeTo(motherFrame);
setUndecorated(true);
setVisible(true);
}
}
Upvotes: 1
Reputation: 12985
This comes from the Oracle javadoc for ProgressMonitor:
Initially, there is no ProgressDialog. After the first millisToDecideToPopup milliseconds (default 500) the progress monitor will predict how long the operation will take. If it is longer than millisToPopup (default 2000, 2 seconds) a ProgressDialog will be popped up.
Note that it doesn't pop up until at least 1/2 second after you create it. Even then, it only pops up if the process is expected to take over 2 seconds.
This is all based on your repeated calls to setProgress(int)
and the time between the progression of values across the range you gave it.
I suspect the conditions that cause the dialog to pop up are not being met. Or, perhaps, your program exits before that amount of time goes by.
Upvotes: 0