i am running a long task on the click of a button. I want to show a message that the task has started. Using swingworker, the JOptionPane creates the message box but its contents are left blank till the task is complete. I guess my EDT is getting blocked and hence the GUI does not get updated unless the task is complete. Is there any way to show this (swingutils.invokelater cannot be used as i need the display at the start of the task) Sample code :-
public class myClass {
private JFrame frame;
private display1 dis;
class display1 extends SwingWorker<Void,Void>
{
public Void doInBackground() throws InterruptedException
{
JOptionPane.showMessageDialog(null,
"Task Started");
return null;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
myClass window = new myClass();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public myClass() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dis=new display1();
dis.execute();
System.out.println("starting");
for(int i=0;i<10000;i++)
System.out.println("this is " +i);// Long task
System.out.println("Finished");
}
});
btnNewButton.setBounds(166, 228, 89, 23);
frame.getContentPane().add(btnNewButton);
}
}