I want to build a JFrame
able to run a background task every time a JButton
was clicked.currently i am using a swing worker and it wont allow the task to be executed more than once.
How can I enable repetition task for SwingWorker
with JButton
click.
public class ScanFileFrame extends JFrame{
JButton btnTicking;
JLabel label1;
ScanFileFrame(){
JFrame jframe = new JFrame();
jframe.setLayout(new FlowLayout());
btnTicking = new JButton("Start Scanning Files");
label1 = new JLabel("No File Scanned");
btnTicking.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
worker.execute();
}
});
jframe.add(btnTicking);
jframe.add(label1);
jframe.setVisible(true);
jframe.setSize(300,300);
}
SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
@Override
protected Boolean doInBackground() throws Exception {
// Simulate scan file
System.out.println("scanning files ....");
Thread.sleep(2000);
return true;
}
//update jframe jlabel when background task finish
protected void done() {
label1.setText("Files Scanned");
System.out.println("complete");
}
};
public static void main(String[] args){
ScanFileFrame f = new ScanFileFrame();
}
}