First of all, I am new to Java: /
I tried to solve this problem myself for about two days, but I can not get around this problem. I am trying to implement a change listener variable. I tried without any success to implement Observer and Observable for my project, but at best I couldnβt put some code elements into while loops, but this fails.
Anyone like this is my class, and if you look at it, I have some global variables defined after the constructor I need to listen to the change in all these global variables, if one of the changes I would like to execute the method.
I was told that JavaFX has methods that can listen to variables, can someone confirm this. In any case, thanks for the 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; } }
java global-variables actionlistener
Sterling duchess
source share