我有一种将数据集导入数据库的方法,我希望有一个进度条来通知用户在单击按钮后正在进行导入。我编写了一个适用于硬编码持续时间的进度条,但我显然希望它是动态的,具体取决于导入的持续时间。我尝试了很多不同的东西都没有成功,所以我希望有一些帮助!:) 谢谢!编辑:这是导入按钮的动作监听器->
btnImportADataset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
progressBar(10);
SystemAdminHome.testMethod();`enter code here`
ExcelReadInReadOut.writeToDB();
}
});
以下是动作监听器中被调用的方法 ->
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();
}