So I have this program that should receive data from a server initially using an objectinputstream. The program should then insert the data received into a ArrayList and update a combo box on the screen with options. All of this works fine I just put it in here for context.
The problem I'm having is that once the the JFrame has loaded I want the GUI to be updated every 10 seconds with new data from the server however it doesn't work. I've tried using swing.timer, ScheduledExecutorService.scheduleAtFixedRate and TimerTask however none seemed to work. the program just freezes (the gui specifically) and no errors are being shown in the console and no exceptions are thrown.
In the code example below I have included my current attempt as well as some of my previous ones as comments.
Code:
constructor and set up function:
public class UserMainGUI extends javax.swing.JFrame {
/**
* Creates new form UserMainGUI
*/
ArrayList<String> data;
JSONParser jsonParser;
ArrayList<String> weatherStationNames;
UserConnection connection;
UpdateDataTimerTask udtt;
Timer timer;
ActionListener taskPerformer;
public UserMainGUI() {
initComponents();
this.data = null;
jsonParser = new JSONParser();
udtt = new UpdateDataTimerTask();
}
public void setupGUI(UserConnection newConnection) //throws InterruptedException
{
connection = newConnection;
if(connection.WeatherStationConnectionCheck())
{
weatherStationOnline.setText("Select a weather station:");
System.out.println("First part working");
data = connection.getWeatherStationData();
if(data != null)
{
parseData();
updateData();
/*taskPerformer = (ActionEvent evt) -> {
data = connection.getWeatherStationData();
System.out.println("updated data: " + data);
parseData();
};
timer = new Timer(10000,taskPerformer);
timer.start(); */
//Thread.sleep(5000);
}
}
else
{
weatherStationComboBox.setVisible(false);
}
}
update data function:
public void updateData()
{
taskPerformer = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
data = connection.getWeatherStationData();
System.out.println("updated data: " + data);
SwingUtilities.invokeLater(() -> {
parseData();
});
}
};
timer = new Timer(10000,taskPerformer);
timer.start();
/*Thread t = new Thread(new Runnable(){
public void run(){
data = connection.getWeatherStationData();
System.out.println("updated data: " + data);
SwingUtilities.invokeLater(() -> {
parseData();
});
try
{
java.lang.Thread.sleep(10000);
}
catch(Exception ex)
{
//System.err.println("Couldn't update data: " ex)
}
}
});
t.start(); */
/*Runnable retrieveRunnable = new Runnable()
{
@Override
public void run() {
try
{
data = connection.getWeatherStationData();
System.out.println("updated data: " + data);
parseData();
}
catch(Exception ex)
{
System.err.println("Could not update data: " + ex);
}
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(20);
executor.scheduleAtFixedRate(retrieveRunnable, 0, 10, TimeUnit.SECONDS); */
}