String or Integer Listener in java?

First off, I'm pretty new to java, and I'm just looking to find out how static classes can interact with non-stationary classes and how to get a static class to update a text field.

I keep looking for information, but can't find anything. I need a variable listener. here is something like what I'm trying to do:

public class Class1{ public static int X; public static void Process(){ for (true){ X = X + 1; } } 

Then I have another class where I want to bind a variable to a text field

 Public class Class2{ ****** On Class1.X.changeValue { Form.jLabel1.setText(Class1.X) } } 

I hope I understand what I'm trying to do. I am trying to bind a label to a variable.

+4
source share
4 answers

There is no language method in Java to listen for variables. You will need to do when your code changes the variable and then update JLabel.
You can have listeners on buttons and other user interface widgets, and they can update JLabel.
One way to implement this is as follows. Do you know about getters and setters? These are the methods that make getting and setting instance variables.

 private int x; public int getX() { return x; } public void setX(int anX) { x = anX; updateLabel("This is x:" + anX) } public void process() { while(true) { int anX = getX(); setX(anX + 1); } } 

You really should try to minimize the use of statics, they tend to encourage "global variables"

+2
source
+3
source

Java itself does not support binding as a function of the language.

JavaFX does, and it certainly interacts very well with Java code.

+1
source

You must give your class your own name (not Class1 ) so that your intent becomes clear. Maybe you want to have a counter ?:

 package so3274211; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class Counter { private int value = 0; private final List<Listener> listeners = new CopyOnWriteArrayList<Listener>(); private void fireAfterValueChanged() { for (Listener listener : listeners) { listener.afterValueChanged(this); } } public int getValue() { return value; } public void increment() { value++; fireAfterValueChanged(); } public void addListener(Listener listener) { listeners.add(listener); } public void removeListener(Listener listener) { listeners.remove(listener); } public interface Listener { void afterValueChanged(Counter counter); } } 

In regular Java code, you cannot directly listen to a variable. But if you insert this variable into a simple object with the appropriate modification methods ( increase() in this case), you can call listeners from this method.

To call a listener, you need to register it somehow. This is usually done using a simple List<Listener> , and the interface to it consists of two methods addListener(Listener) and removeListener(Listener) . This pattern can be found throughout AWT and Swing.

I defined the Listener interface inside the Counter class, because this interface does not really matter outside this class, and I did not want to call it CounterListener . Thus, the number of .java files is less.

Using this counter in a real program might look like this:

 package so3274211; public class Main { public static void main(String[] args) { Counter c = new Counter(); c.increment(); c.addListener(new Counter.Listener() { @Override public void afterValueChanged(Counter counter) { System.out.println(counter.getValue()); } }); c.increment(); } } 

First I created a counter, and then added a listener to it. The expression new Counter.Listener() { ... } is an anonymous class definition that also often appears in Java GUI programming.

So, if you are serious about programming using the graphical interface, you need to learn these concepts and implementation methods (encapsulating a variable in a class, adding a listener code, defining a listener, calling a listener from code that changes the variable) anyway.

0
source

Source: https://habr.com/ru/post/1316053/


All Articles