Note that there are some explanatory texts on larger screens.

plurals
  1. POSwing JProgressBar for time spent in method
    primarykey
    data
    text
    <p>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 -></p> <pre><code> btnImportADataset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { progressBar(10); SystemAdminHome.testMethod();`enter code here` ExcelReadInReadOut.writeToDB(); } }); </code></pre> <p>The following is the method being called in the action listener -></p> <pre><code> 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 = "&lt;html&gt;" + "&lt;font color=\"#FF0000\"&gt;" + "&lt;b&gt;" + "Import completed." + "&lt;/b&gt;" + "&lt;/font&gt;" + "&lt;/html&gt;"; label.setText(str); } i = i + 1; progBar.setValue(i); } }); } public static void testMethod() { btnImportADataset.setEnabled(false); i = 0; String str = "&lt;html&gt;" + "&lt;font color=\"#008000\"&gt;" + "&lt;b&gt;" + "Import is in process......." + "&lt;/b&gt;" + "&lt;/font&gt;" + "&lt;/html&gt;";label.setText(str);timer.start(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload