I want to update my Swing Gui every 30 seconds, where some labels shall get new values from a DB connection. So far, I tried to create a new Gui everytime and dispose the old one, but this is no ellegant solution and did not work anyway.
public class Gui extends JFrame {
private boolean open;
//setter, getter
public Gui() {
JPanel pane = new JPanel(new BorderLayout());
int numberOfRows = 9;
int numberOfColumns = 2;
pane.setLayout(new GridLayout(numberOfRows, numberOfColumns));
String wState;
if(open){wState="Window open";}
else{wState="Window closed";}
JLabel windowState= new JLabel(wState);
pane.add(windowState);
new Timer(5000, new WindowState()).start(); }
private class WindowState implements ActionListener {
public void actionPerformed(ActionEvent e) {
Gui g = new Gui();
g.setOpen(true);
}
I know it does not work like this but I hope it becomes clear what I want to do. The problem is that I cannot access the Gui elements in the actionPerformed() method. I simply want to update the windowState Label with the new Value retrieved in the actionPerformed() method.