我想在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条记录,然后我想用进度条显示的过程,但它不工作。我错过了什么?请帮助我。
progressbar在public void run(){} – 2014-09-24 16:44:54
@MuhammadHaryadiFutra:是的,现在你已经添加了它。事实上,你应该把你的整个方法体放在'Runnable'(甚至是带有'ProgressBar'的部分)并运行它。 – Dici 2014-09-24 16:47:33
我不知道该怎么做,我卡住了 – 2014-09-24 16:59:23