Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:27:25+00:00 2026-05-27T15:27:25+00:00

First of all i am brand new to Java : / I have been

  • 0

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;
    }




}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:27:25+00:00Added an answer on May 27, 2026 at 3:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, let me say I am very new to rails, have been
First of all, I know how to build a Java application. But I have
First I'm brand new to JS but have an idea that object classes are
brand new on this forum and this is my first post! At work we're
First of all: I am not an experienced ClearCase user, but I have lots
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
First of all there is a partial question regarding this, but it is not
First of all, let's define a few tables: Users table will store information about
I have a problem with the new DataGrid component that comes with .NET4. The
i know im not the first with this problem, but i cant seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.