Pete
Pete

Reputation: 514

Access Gui in ActionListener Class to update it every x seconds with Timer

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.

Upvotes: 0

Views: 210

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208984

"The problem is that I cannot access the Gui elements in the actionPerformed() method."

You need to learn about variable scope. Currently, all your variables are scoped locally in the constructor

public class GUI {
    public GUI {
        JPanel panel = new JPanel();  <--- Locally scoped
    }
}

You need to give your object that you want to access, a global scope

public class GUI {
    JPanel panel= new JPanel();     <--- global scope

    public GUI(){
    }
}

You can then access panel in the actionPerformed


See here

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class GUI extends JFrame {

    private boolean open;
    JPanel pane;
    JLabel windowState;
    int count = 1;

    public GUI() {

        int numberOfRows = 9;
        int numberOfColumns = 2;
        pane = new JPanel(new BorderLayout());
        pane.setLayout(new GridLayout(numberOfRows, numberOfColumns));

        String wState;
        if (open) {
            wState = "Window open";
        } else {
            wState = "Window closed";
        }

        windowState = new JLabel(wState);
        pane.add(windowState);

        add(pane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        new Timer(500, new WindowState()).start();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GUI();
            }
        });
    }

    private class WindowState implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            count++;
            windowState.setText("Number " + count + "!");
        }
    }
}

Upvotes: 1

Related Questions