I want to fool arround with Java and especially the Swing library. I am having difficulies getting the hang of it tho.
On press of a button I am adding a new JInternalPanel to my UI which inside the constructor makes a 3rd party API call. The InternalFrame is not showing up for the duration of the API call, so basically the UI freezes completely for a few seconds.
How can this be solved? Is there any recommended way to archieve that?
Here is my simple code
public MarketView(){
setTitle(TITLE);
setSize(DEFAULT_SIZE);
setMaximizable(true);
setClosable(true);
setIconifiable(true);
setLayout(new BorderLayout());
//this line here is causing the gui to freeze for duration of the 3rd party API call.
System.out.println(this.api.getRestClient().getAllAssets().toString());
show();
}
Updated my code based on some recommendations, still not sure if this is the proper way. Looks pretty verbose and somehow unesthetic to me.
public MarketView(){
setTitle(TITLE);
setSize(DEFAULT_SIZE);
setMaximizable(true);
setClosable(true);
setIconifiable(true);
setLayout(new BorderLayout());
//this line here is causing the gui to freeze for duration of the 3rd party API call.
new SwingWorker() {
APIManager api = APIManager.getInstance();
@Override
protected List<TickerStatistics> doInBackground() throws Exception {
List<TickerStatistics> ticker = api.getRestClient().getAll24HrPriceStatistics();
return ticker;
}
@Override
protected void done() {
try {
List<TickerStatistics> ticker = (List<TickerStatistics>) get();
System.out.println(ticker);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
System.out.println("Done");
}
}.execute();
show();
}