I have a swing application. Upon clicking a menuItem on the menu bar, I am trying to make an API call which will supposedly take some time.
I didn't want the Swing app to get stuck/hanged until the API call gives its result so I have used the SwingUtilities.invokeLater to separate out a newly spawned thread that will take care of API call. The following is the code for the above
public void actionPerformed(ActionEvent e){
Object src=e.getSource();
if(src.equals(theJMenuItemClicked)){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//apicall here
}
})
}
As long as the API call thread is functioning I show a JDIalog with a message saying "API call in Progress" and an "Abort Thread" button.
I would like to kill the thread upon clicking the "ABort Thread" button in the Jdialog.
If it is a normal thread, we have a Thread t = new Thread(new Runnable())
and we invoke a t.stop
on the same.
How do I get hold of that particular thread instance spawned by SwingUtilities so that I can call a stop on it? In simpler words how can I kill the above thread created?