Source Code : Use SwingWorker to perform background tasks
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Use SwingWorker to perform background tasks
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
public class Main extends JFrame {
public Main() {
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton processButton = new JButton("Start");
JButton helloButton = new JButton("Hello");
processButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MyTask process = new MyTask();
try {
process.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
});
helloButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello There");
}
});
this.getContentPane().add(processButton);
this.getContentPane().add(helloButton);
this.pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
class MyTask extends SwingWorker {
protected Object doInBackground() throws Exception {
Integer result = new Integer(0);
for (int i = 0; i < 10; i++) {
result += i * 10;
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
Thank with us