我使用的是netbeans,这是按钮点击函数。我希望进度条在我点击按钮后立即运行。模式将是不确定的,因为我正在调用一个类WriteToFile
,它在后台工作几分钟,不返回任何值。
private void createFileButtonActionPerformed(java.awt.event.ActionEvent evt)
{
String str = (String) selectTypeComboBox.getSelectedItem();
createFileButton.setEnabled(false);// Prevent more button presses
String filepaths = fileChooserWindow.getSelectedFile().getAbsolutePath();
String selectType =(String) selectTypeComboBox.getSelectedItem();
WriteToFile writeOBJ;
writeOBJ = new WriteToFile(filepaths,start,selectType);
try
{
writeOBJ.selectType();
}
catch (SQLException | IOException | RuntimeException ex)
{
System.exit(0);
}
}
实现了以下功能:
jProgressBar1.setVisible(true);
jProgressBar1.setIndeterminate(true);
Executor executor = java.util.concurrent.Executors.newSingleThreadExecutor();
executor.execute(new Runnable(){
@Override
public void run(){
String str = (String) selectTypeComboBox.getSelectedItem();
createFileButton.setEnabled(false);// Prevent more button presses
String filepaths = fileChooserWindow.getSelectedFile().getAbsolutePath();
String selectType =(String) selectTypeComboBox.getSelectedItem();
WriteToFile writeOBJ;
writeOBJ = new WriteToFile(filepaths,start,selectType);
try
{
writeOBJ.selectType();
}
catch (SQLException | IOException | RuntimeException ex)
{
System.exit(0);
}
jProgressBar1.setVisible(false);
}