Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Listen to variable change

First of all i am brand new to Java : /

I have been trying to solve this problem on my own for about 2 days now but cant get around it the problem is i am trying to implement a variable change listener. I have tried without successes to implement Observer and Observable to my project but whit no successes at best i came up by wrapping some elements of the code in to while loops but that well fails.

Any how this is my class and if you look at it i have some global variables defined after the constructor i need to listen for a change in all of those global variables if one changes i would like to execute a method.

I have been told JavaFX has methods that can listen to variables can someone confirm this. Anyhow thanks for help in advance.

public class Tower_Controller {

    public Tower_Controller() {
    }

    //Global variables
    String isSelected = null;
    int hasModules = 0;
    int cap_s = 0;
    int cpu_s = 0;
    int cap = 0;
    int cpu = 0;
    int shield = 0;
    int armor = 0;
    double em = 00.00;
    double th = 00.00;
    double ki = 00.00;
    double ex = 00.00;


    public void invoke() {
        Invoke_GUI runnable = new Invoke_GUI();
        final JLabel tower_name = runnable.tower_name;
        final JComboBox tower_select = runnable.tower_select;
        final JTree module_browser = runnable.module_browser;
        final JTree selected_modules = runnable.selected_modules;

        final JProgressBar cap_bar = runnable.cap_bar;
        final JProgressBar cpu_bar = runnable.cpu_bar;

        final JLabel em_res = runnable.em;
        final JLabel th_res = runnable.thermic;
        final JLabel ki_res = runnable.kinetic;
        final JLabel ex_res = runnable.explosive;

        tower_select.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (isSelected != null) {
                    Events evt = new Events();
                    evt.towerSelected(isSelected);
                } else {
                    tower_name.setText(tower_select.getSelectedItem().toString());
                    isSelected = tower_name.toString();
                }
            }
        });

        removeTower(tower_name);
        runnable.setVisible(true);

    }



    public void updateValues(final JProgressBar cap_bar, final JProgressBar cpu_bar, final JLabel em_res,
        final JLabel th_res, final JLabel ki_res, final JLabel ex_res){

        cap_bar.setMaximum(cap);
        cap_bar.setString(cap_s + " / " + cap);
        cap_bar.setStringPainted(true);

        cpu_bar.setMaximum(cpu);
        cpu_bar.setString(cpu_s + " / " + cpu);
        cpu_bar.setStringPainted(true);

        String em_v = String.valueOf(em); 
        em_res.setText(em_v);

        String th_v = String.valueOf(th); 
        th_res.setText(th_v);

        String ki_v = String.valueOf(ki); 
        ki_res.setText(ki_v);

        String ex_v = String.valueOf(ex); 
        ex_res.setText(ex_v);
    }

    public void updateList(final ArrayList<String> nodes, final JTree selected_modules) {
        DefaultMutableTreeNode nod = new DefaultMutableTreeNode();
        for (int i = 0; i < nodes.size(); i++) {
            nod.add(new DefaultMutableTreeNode(nodes.get(i)));
        }
        selected_modules.setModel(new DefaultTreeModel(nod));
    }

    public void removeTower(final JLabel tower_name) {
        tower_name.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (hasModules == 1 & isSelected != null) {
                    Events evt = new Events();
                    evt.towerHasModules();
                } else if (isSelected == null) {
                } else {
                    tower_name.setText("No Control Tower selected");
                    isSelected = null;
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }

    public JLabel setTowerName(JLabel a, String name) {
        a.setText(name);
        return a;
    }




}
like image 643
Sterling Duchess Avatar asked Feb 02 '23 10:02

Sterling Duchess


1 Answers

The general procedure to be notified of a change to a variable is as follows:

  1. First, make the variables private.
  2. Create two methods for each variable, one which sets its value to an argument (often called setX(), where X is the variable name), the other which retrieves its value (getX())
  3. Everywhere you need to read or set the variable, call the methods instead.
  4. In the setX() method, call notifyObserver() on your Observers, in a loop.

And there you go! Now every time the variable is changed, registered Observers are notified. The key part of this solution is that the variables have to be private, so that no code can set their values without going through the setX() methods.

like image 167
Ernest Friedman-Hill Avatar answered Feb 12 '23 08:02

Ernest Friedman-Hill