私のJavaGUIアプリには、がJButton
あり、クリックすると、function
に接続するためにadatabase
を呼び出し、次に、内のafunction
を呼び出し、次に、あるファイルからテキストを読み取るaを呼び出し、別のファイルからテキストを読み取るaを呼び出してロードします。両方から、そしてDB内のまたはデータのいずれかを呼び出します。これらはすべて正常に機能します。clear
table
DB
function
variables
function
data
function
update
insert
しかし、私の質問はに関連してJButton
います。クリックすると、ユーザーが作業が行われていることを認識できるように実行したいのですが、Indeterminate progress bar
それが終了する直前にaction listener setIndeterminate to false
、の値をに設定しますprogress bar
が100(complete)
、私の場合はクリックするbutton
とクリック状態のままでprogress bar
フリーズします。
これを防ぐために何を実装する必要がありますか?おそらく糸脱毛?しかし、私はJavaでのスレッド化にまったく慣れていません。これが私のアクションリスナーです:
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == genButton )
{
progressBar.setIndeterminate(true);
progressBar.setString(null);
try
{
dbConnect(); //connects to DB
clearSchedules(); // deletes data in tables
readFile(); // reads first file and calls the other functions
dbClose();// closes the DB
progressBar.setIndeterminate(false);
progressBar.setValue(100);
}
catch (Exception e1){
System.err.println("Error: " + e1.getMessage());
}
}
}
}
ちなみに、プログラムの進行に合わせて実際にアクションバーを動かしてもらいたいのですが、進行状況を監視する方法がわかりませんでした。
ありがとう、ビーフ。
ここでの更新 は、SwingWorkerの私の例とその使用方法です。
グローバルに宣言
private functionWorker task;
private abstract class functionWorker extends SwingWorker {
public void execute() {
try {
dbConnect();
} catch (SQLException e) {
e.printStackTrace();
}
clearSchedules();
try {
readFile();
} catch (IOException e) {
e.printStackTrace();
}
dbClose();
}
}
私のactionPerformedメソッドの内部
if( e.getSource() == genButton )
{
progressBar.setIndeterminate(true);
progressBar.setString(null);
try
{
task.execute();
progressBar.setIndeterminate(false);
progressBar.setValue(100);
}
catch (Exception e1){
System.err.println("Error: " + e1.getMessage());
}
}