2014-09-24 50 views
0

我想在java GUI中显示进度条上的进度,但进程在进程完成之前不显示任何内容。如何在java中使用进度条?

这是我的尝试:

public void selectTweet(){ 
    Connection conn = null; 
    Statement stmt = null; 
    try{ 
     Class.forName("com.mysql.jdbc.Driver"); 
     conn = Drive 

     rManager.getConnection(DB_URL, USER, PASS); 
     stmt = conn.createStatement(); 

     //menghitung jumlah query pada table tweet 
     String sql = "SELECT COUNT(*) FROM tweet"; 
     ResultSet rs=stmt.executeQuery(sql); 
     int row = 0; 
     while(rs.next()){ 
      row = rs.getInt("COUNT(*)"); 
     } 

     int initial_number = Integer.parseInt(jTextField4.getText()); 

     for(int i=initial_number;i<row;i++){ 
      System.out.println("tweet ke-"+i); 
      String sql1 = "SELECT tweet,date,location FROM tweet WHERE tweet_id='"+i+"'"; 
      ResultSet rs2 = stmt.executeQuery(sql1); 

      while(rs2.next()){ 
       tweet = rs2.getString("tweet"); 
       date = rs2.getString("date"); 
       location = rs2.getString("location"); 
      } 
      System.out.println("Tweet: " + tweet); 
      removeStopWords(tweet, i, date, location); 


      final int currentValue = i; 
      try{ 
       SwingUtilities.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
         jProgressBar1.setValue(currentValue); 
        } 
       }); 
       java.lang.Thread.sleep(100); 
      }catch (InterruptedException e) { 
       //JOptionPane.showMessageDialog(frame, e.getMessage()); 
      } 
     } 
    }catch(SQLException se){ 
     //Handle errors for JDBC 
     se.printStackTrace(); 
    }catch(Exception e){ 
     //Handle errors for Class.forName 
     e.printStackTrace(); 
    }finally{ 
     //finally block used to close resources 
     try{ 
      if(stmt!=null) 
      conn.close(); 
     }catch(SQLException se){}// do nothing 
     try{ 
      if(conn!=null) 
      conn.close(); 
     }catch(SQLException se){ 
      se.printStackTrace(); 
     }//end finally try 
    }//end try 
} 

我想要做的是从数据库中删除禁用词中字符串的记录,有5000条记录,然后我想用进度条显示的过程,但它不工作。我错过了什么?请帮助我。

回答

0

您需要在单独的Thread中处理您的任务。如果您不这样做,则会阻止调用repaint方法的主Swing线程。另外,我在代码中看不到任何ProgressBar,您必须在执行任务的某个步骤时更新其值。

SwingUtilities.invokeLater(
    new Runnable(
     public void run() { 
      for(int i=initial_number;i<row;i++){ 
       System.out.println("tweet ke-"+i); 
       String sql1 = "SELECT tweet,date,location FROM tweet WHERE tweet_id='"+i+"'"; 
       ResultSet rs2 = stmt.executeQuery(sql1); 

       while(rs2.next()){ 
        tweet = rs2.getString("tweet"); 
        date = rs2.getString("date"); 
        location = rs2.getString("location"); 
       } 
       System.out.println("Tweet: " + tweet); 
       removeStopWords(tweet, i, date, location); 

       jTextArea1.append(toTextArea); 
       // the JProgressBar must have been instantiated 
       // as new JProgressBar(initial_number,row - 1) 
       jProgressBar1.setValue(i); 
      } 
    })); 
+0

progressbar在public void run(){} – 2014-09-24 16:44:54

+0

@MuhammadHaryadiFutra:是的,现在你已经添加了它。事实上,你应该把你的整个方法体放在'Runnable'(甚至是带有'ProgressBar'的部分)并运行它。 – Dici 2014-09-24 16:47:33

+0

我不知道该怎么做,我卡住了 – 2014-09-24 16:59:23

0

对长时间运行的任务使用SwingWorker可以使事情变得更简单。

public void selectTweet() 
{ 
    final int initial_number = Integer.parseInt(jTextField4.getText()); // This should be done outside of the worker because you are accessing a UI element's data. 
    SwingWorker<Void, int[]> worker = new SwingWorker<Void, int[]>() 
    { 
     @Override 
     protected Void doInBackground() throws Exception 
     { 
      Connection conn = null; 
      Statement stmt = null; 
      try 
      { 
       Class.forName("com.mysql.jdbc.Driver"); 
       conn = DriverManager.getConnection(DB_URL, USER, PASS); 
       stmt = conn.createStatement(); 

       String sql = "SELECT COUNT(*) FROM tweet"; 
       ResultSet rs = stmt.executeQuery(sql); 
       int row = 0; 
       while(rs.next()) 
       { 
        row = rs.getInt("COUNT(*)"); 
       } 

       for(int i = initial_number; i < row; i++) 
       { 
        System.out.println("tweet ke-" + i); 
        String sql1 = "SELECT tweet,date,location FROM tweet WHERE tweet_id='" + i + "'"; 
        ResultSet rs2 = stmt.executeQuery(sql1); 

        String tweet, date, location; 
        while(rs2.next()) 
        { 
         tweet = rs2.getString("tweet"); 
         date = rs2.getString("date"); 
         location = rs2.getString("location"); 
        } 
        System.out.println("Tweet: " + tweet); 
        removeStopWords(tweet, i, date, location); 

        publish(new int[]{i,row}); 
       } 
      } 
      catch(SQLException se) 
      { 
       //Handle errors for JDBC 
       se.printStackTrace(); 
      } 
      catch(Exception e) 
      { 
       //Handle errors for Class.forName 
       e.printStackTrace(); 
      } 
      finally 
      { 
       //finally block used to close resources 
       try 
       { 
        if(stmt != null) 
         conn.close(); 
       } 
       catch(SQLException se) 
       { 
       }// do nothing 
       try 
       { 
        if(conn != null) 
         conn.close(); 
       } 
       catch(SQLException se) 
       { 
        se.printStackTrace(); 
       }//end finally try 
      }//end try 
     } 

     @Override 
     protected void process(List<int[]> chunks) // Run in the EDT, so no invokeLater needed 
     { 
      if(chunks == null || chunks.isEmpty()) 
       return; 
      int[] last = chunks.get(chunks.size() - 1); 
      jProgressBar1.setMinimum(0); 
      jProgressBar1.setMaximum(last[1] - initial_number); 
      jProgressBar1.setValue(last[0] - initial_number); 
     } 

     @Override 
     protected void done() // Run in the EDT, so no invokeLater needed 
     { 
      jProgressBar1.setValue(jProgressBar1.getMaximum()); 
     } 
    }; 
    worker.execute(); 
}