0

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.

Pete
  • 502
  • 1
  • 6
  • 20

1 Answers1

1

"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 + "!");
        }
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • It would need to be marked as `final`, however, to have access from within the ActionListener inner class. – Gorbles Feb 04 '14 at 13:48
  • 1
    @Gorb _who says?_. You should test out your theories before publicly posting them. They only need to be final if declared in the constructor and are being accesses from an inner _annonymous_ class _also_ in the constructor. Neither are the case here. – Paul Samsotha Feb 04 '14 at 13:55
  • I sit corrected! I missed that you instantiated it in the same line. Still, attitude yo :/ – Gorbles Feb 04 '14 at 13:57
  • @Gorb *"Still, attitude yo :/"* It's OK. Folks are very forgiving of a petulant, 'I am a precious snowflake' attitude like you just displayed. Chill.. ;) – Andrew Thompson Feb 04 '14 at 14:37
  • I'm confused, I displayed no attitude. I meant the correction sincerely. Sorry for derailing this answer, folks. – Gorbles Feb 04 '14 at 16:03