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();
}