So I am trying to use SwingWorkers in my application (written in Java) where I collect some data from an API and display it in the app’s GUI. Please keep in mind that I am very unsure when it comes to threads, it’s a concept I struggle to grasp and understand.
I will provide the method for which the thread is being run, I’m not sure what else I need to provide but do let me know if there’s more that needs to be shown in order for me to get help.
private void initialThread() {
dataUpdateTimer = new Timer(60 * 60 * 1000, new ActionListener() { // Update every hour
@Override
public void actionPerformed(ActionEvent e) {
rg.getLoadingMsg(); // Display the JOptionPane message
if (cachedChannelData != null) { //If the cached data is not null
cachedChannelData.clear(); //Clear the cached data to make new cache
}
rg.clearChannelTable(); //Clear the table displaying the channels (GUI)
rg.clearGui(); //Clear the rest of the GUI
SwingWorker<Void, Map<String, Map<String, programClass>>> sw = new SwingWorker<Void, Map<String, Map<String, programClass>>>() {
@Override
protected Void doInBackground() throws Exception {
cachedChannelData = cm.loadChannels(); //Get the channels (data retrieved from API)
publish(cachedChannelData); //Send the data that's been collected
return null; // Ensure doInBackground returns null as specified by its return type
}
@Override
protected void process(List<Map<String, Map<String, programClass>>> chunks) {
Map<String, Map<String, programClass>> result = chunks.get(chunks.size() - 1);
for (Map.Entry<String, Map<String, programClass>> channelEntry : result.entrySet()) { //For every channel data retrieved from the API, do the following
String cname = cm.getChannelName(channelEntry.getKey()); //Get the channel's name
rg.displayChannels(cname, channelEntry.getKey()); //Add that channel to the table to display it
Map<String, programClass> programs = channelEntry.getValue(); //List of programs in that channel retrieved from the API
if(programs != null)
{ //If there are programs in the channel, hence programs != null
for (Map.Entry<String, programClass> programEntry : programs.entrySet()) { //For every program in the channel
programClass program = programEntry.getValue(); //Get a variable containing info about that program
String description = program.getDescription(); // Get the description from the program
JLabel image = program.getDisplayImage(); // Get the image JLabel from the program
String start = program.getStart(); //Get the start time of the program
String end = program.getEnd(); //Get the end time of the program
String name = program.getName(); //Get the name of the program
// Assuming you have a method to display each program's description and image individually
rg.displayPrograms(description, image, name, start, end); // Display the programs
}
rg.clearGui(); //Clear the GUI of it's all old displayed data
}
}
}
};
sw.execute();
}
});
dataUpdateTimer.setInitialDelay(0); // Start timer immediately
dataUpdateTimer.start();
}
Here’s the order in which I want my application to run:
- Display a message (JOptionPane)
- Clear the GUI
- Collect the data
- Display the data on screen