1

I have a method that imports a dataset into a database, I want to have a progress bar to inform the user that the import is occurring after clicking on the button. I have coded a progress bar that works for a hardcoded duration, but I obviously want this to be dynamic depending on the duration of the import. I have tried a lot of different things with no success, so I hope there is some help out there! :) Thanks! Edit: this is the action listener for the import button ->

        btnImportADataset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                progressBar(10);
                SystemAdminHome.testMethod();`enter code here`
                ExcelReadInReadOut.writeToDB();
            }
        });

The following is the method being called in the action listener ->

    private final static int interval = 1000;
    private static int i;
    private static JLabel label;
    private static JProgressBar progBar;
    private static Timer timer;

    public static void progressBar(final long duration) {
        JFrame frame = new JFrame("Progress Bar");
        ProgressBar progBarInstance = new ProgressBar();

        progBar = new JProgressBar(0, (int) duration);
        progBar.setValue(0);
        progBar.setStringPainted(true);

        label = new JLabel("Import a dataset");

        JPanel panel = new JPanel();
        //panel.add(btnStart);
        panel.add(progBar);

        JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());
        panel1.add(panel, BorderLayout.NORTH);
        panel1.add(label, BorderLayout.CENTER);
        panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        frame.setContentPane(panel1);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a timer.
        timer = new Timer(interval, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (i == duration) {
                    timer.stop();
                    btnImportADataset.setEnabled(true);
                    progBar.setValue(0);
                    String str = "<html>" + "<font color=\"#FF0000\">" + "<b>"
                            + "Import completed." + "</b>" + "</font>"
                            + "</html>";
                    label.setText(str);
                }
                i = i + 1;
                progBar.setValue(i);
            }
        });


    }

    public static void testMethod() {
        btnImportADataset.setEnabled(false);
        i = 0;
        String str = "<html>" + "<font color=\"#008000\">" + "<b>"
                + "Import is in process......." + "</b>" + "</font>"
                + "</html>";label.setText(str);timer.start();
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1898230
  • 75
  • 3
  • 11
  • 2
    could you show what you tried? I think you have to use `SwingWorker` and a `PropertyChangeListener` on the fly – nachokk Jun 13 '13 at 18:43
  • You can set the progress bar to `indeterminate` mode and not have to worry about how long it takes. See: http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html#indeterminate – Eric Jun 13 '13 at 19:02

2 Answers2

3

See https://stackoverflow.com/a/10774277/66207 use a SwingWorker's doInBackground method to do the heavy load and use the publish method to publish the progress.

[Edit] linked to the correct answer instead of the question.

Community
  • 1
  • 1
keuleJ
  • 3,418
  • 4
  • 30
  • 51
-1

You can try somethig like that (not tested):

JProgressBar jp = new JProgressBar();
jp.setMinimum(System.currentTimeMillis());

while(import) {
    jp.setValue(System.currentTimeMillis());
}
Edward Falk
  • 9,991
  • 11
  • 77
  • 112
TroyAndAbed
  • 301
  • 1
  • 10
  • 3
    You sould update the gui in the EventDispatchThread and do the work in another thread otherwise your GUI freezes up. – keuleJ Jun 13 '13 at 18:56