0

In my swings windows application after click the run button some operation is executed. If i am try to close window when operation is still in progress, close operation is not working. After complete the execution of process then only close window operation is working. Otherwise it will not responds the close operations.

I have already tried below mentioned codes. But that one is not stopped working

addWindowListener(new WindowAdapter()
 {
   public void windowClosing(WindowEvent we) 
   {
    System.exit(0); 
            or
    System.exit(1);
            or
    setDefaultCloseOperation(EXIT_ON_CLOSE);     
            or
    setDefaultCloseOperation(3);
                or
        dispose();
    }

}); 
    JButton jb = new JButton("Run");        
    add(jb);         
    jb.setBounds(10, 30, 100, 30);        
    setSize(150,150);  
    setLayout(null);  
    setVisible(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
      System.exit(0);
    }
    });
    jb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // some operations . For Example here i'm add 1E5 data,s in my collection object. again i will replace data's places of each element.
            for(int i=0;i<1E5;i++)
            {
                ll.add(i);
            }
            for(int i=0;i<1E5;i++)
            {
                ll.add(1,i);
            }
            for(int i=0;i<1E5;i++)
            {
                ll.add(0,i);
            }
            System.out.println(ll);
        }
    });

If I am clicking close button, my window terminate the currently executed process and Close the window.

Gonzo345
  • 1,133
  • 3
  • 20
  • 42
  • 2
    have you tried having the running action being done in a seperate thread? – Stultuske Jan 24 '19 at 10:16
  • Could you please provide [MCVE](https://stackoverflow.com/help/mcve) – Akiner Alkan Jan 24 '19 at 10:16
  • Stultuske I am not familiar with thread. so please provide me some example code for that process. – Bhuvana Mini Jan 24 '19 at 10:24
  • see [General Information about Writing Event Listeners](https://docs.oracle.com/javase/tutorial/uiswing/events/generalrules.html): "The most important rule to keep in mind about event listeners is that they should execute very quickly." and it goes on to say: "For help on using threads, see [Concurrency in Swing.](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html)" – Hulk Jan 24 '19 at 10:29
  • Possible duplicate of [How do I use SwingWorker in Java?](https://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java) – Hulk Jan 24 '19 at 10:36
  • The [JavaDocs for `SwingWorker`](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/SwingWorker.html) also contain pretty extensive explanations and sample code. – Hulk Jan 24 '19 at 10:45
  • Swing worker is working. Thank You – Bhuvana Mini Jan 27 '19 at 12:49

1 Answers1

0
class MyThread extends Thread {      
    public void run() {
        // some operations . For Example here i'm add 1E5 data,s in my collection object
        ..
        ..        
    }
}

Then in your actionPerformed method on your JButton actionlistener you just start the thread :

public void actionPerformed(ActionEvent e) {
    new MyThread().start();
}
nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • while true in general, swing has a special `SwingWorker` class specifically designed for this purpose, which provides built-in functionality to report back to the `EventThread` when done and for safely publishing intermediate results. – Hulk Jan 24 '19 at 10:40
  • @Hulk I fully agree, however as the OP mentioned he's not even familiar with threads I just posted the most simple solution – nullPointer Jan 24 '19 at 11:05
  • Yes, but that would require to handle synchronization etc. on their own, which is probably more tricky than using something that already handles some of this under the hood. – Hulk Jan 24 '19 at 11:08