views:

37

answers:

2

i have a program that takes some time creating pdf files i would like to show progress to user

when i finish making a pdf file i try to call the progressbar to update its status:

ProgressDialog progress = new ProgressDialog(instance, numberOfInvoices);
progress.setVisible(true);
progress.setAlwaysOnTop(true);

for(int i = 0 ; i<numOfPdfs ; i++){
    progress.change(i + 1);
}

the progressdialog looks like this:

public class ProgressDialog extends JDialog {

    private JProgressBar progressBar;

    public ProgressDialog(Window parent, int aantal) {
        super(parent);

        this.setPreferredSize(new Dimension(300, 150));

        progressBar = new JProgressBar(0, aantal);
        progressBar.setValue(0);
        progressBar.setSize(new Dimension(280, 130));
        this.add(progressBar, BorderLayout.CENTER);

        this.pack();
        this.setLocationRelativeTo(parent);
    }

    public void change(int aantal) {
        if (aantal < progressBar.getMaximum() && aantal >= 0) {
            progressBar.setValue(aantal);
        }
    }
}

all i get is an empty window (white)

i looked around on this forum but the treads solution seems soo complex

between calculation of 2 pdf files i should be able to update the gui, right?

ok colin

i tried this: (complete class)

public class ProgressDialog extends JDialog {

    private JProgressBar progressBar;
    private String message;
    private static int number;

    public ProgressDialog(Window parent, int max, String message) {
    super(parent);

    this.message = message;
    this.setPreferredSize(new Dimension(300, 150));

    progressBar = new JProgressBar(0, max);
    progressBar.setValue(0);
    progressBar.setSize(new Dimension(280, 130));
    progressBar.setString(message + " 0/" + progressBar.getMaximum());
    progressBar.setStringPainted(true);
    this.add(progressBar, BorderLayout.CENTER);

    this.pack();
    this.setLocationRelativeTo(parent);

    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));


    }

    public void change(int number) {
    ProgressDialog.number = number;
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
        int number = ProgressDialog.getAantal();
        if (number < progressBar.getMaximum() && number >= 0) {
            progressBar.getModel().setValue(number);
            progressBar.setString(message + " " + progressBar.getValue() + "/" + progressBar.getMaximum());
            System.out.println("progressbar update: " + number + "/" + progressBar.getMaximum());
        } else {
            setCursor(null);
            System.out.println("progressbar terminated");
        }
        }
    });
    }

    public static int getAantal(){
    return number;
    }
}

now it still shows the empty window but when all pdf's are ready it does show the progressbar with 0% completed then it closes (like it should do)

any idea why it stays blank during process?

+2  A: 

You do need to do it with threads. Better learn it than escape, because it's a common GUI framework design.

It can be as simple as:

public void change(int aantal) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            if (aantal < progressBar.getMaximum() && aantal >= 0) {
                progressBar.setValue(aantal);
            }
        }
    });
}

Or replace invokeLater() with invokeAndWait().

Konrad Garus
A: 

Read the section from the Swing tutorial on Concurrency to understand why Threads are needed for this problem. Also read the section on "How to Use Progress Bars" for a working example.

camickr