单击按钮时具有不确定模式的进度条不起作用

时间:2015-07-01 18:39:14

标签: java netbeans jprogressbar

我正在使用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);
    }
}

1 个答案:

答案 0 :(得分: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);
   }