views:

66

answers:

3

First off, I am pretty new to java and I am just staring to get the hang of how static classes can interface with non-static classes and how to get a static class to update a textbox.

I keep searching for information, but I cannot find anything. I need a variable listener. here is something like what I am 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 textbox

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

I hope I was clear on what I am trying to do. I am trying to bind a label to a variable.

A: 

In Java there is no language specific way to have listeners on variables. What you will need to do is when your code changes the variable then have it also update the JLabel.
You can have listeners on buttons and other UI widgets and they might update the JLabel.
One way you might implement this is as follows. Do you know about getters and setters ? They are methods that do the getting and setting of 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 should really try to minimize the use of statics as much as possible, They tend to encourage "Global Variables"

Romain Hippeau
My processes thus far have required static processing. I've defined 200 static variables and 15 static classes which there are only a single instance of. Multiple instances will screw things up. Basically, it's a serial communications application. I appreciate the help.
Adam Outler
@Adam Outler I have written Serial communications software without that many static variables. It sounds like you are just writing procedural code, This will be tricky when your program gets large. OO principles are a great way to encapsulate and hide information from other parts of the program.
Romain Hippeau
Most likely you were working with the same information over and over. My application is collecting data which is used for operation of several systems. RPM, timing, temperatures, flow rates, saturation levels, each one is a separate static variable and only the most current information is important.
Adam Outler
+1  A: 

Java itself doesn't support binding as a language feature.

JavaFX does, and it interfaces with Java code very smoothly, of course.

erickson
A: 

You should give your class a proper name (not Class1), so that your intention 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 ordinary Java code you cannot listen to a variable directly. But if you put that variable into a simple object with proper modification methods (increase() in this case), you can call the listeners from this method.

To call a listener you have to somehow register it. This is usually implemented with a simple List<Listener>, and the interface to that consists of the two methods addListener(Listener) and removeListener(Listener). You can find this pattern everywhere in AWT and Swing.

I have defined the Listener interface inside the Counter class because this interface doesn't have much value outside that class, and I didn't want to call it CounterListener. That way, there are fewer .java files flying around.

Using that counter in an actual program may 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();
  }

}

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

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

Roland Illig
wow.. that's way too much information for me to process. I can't see the forrest through the trees. I had a simple problem and I wrote a very simple example to show the simple problem.
Adam Outler
@Adam Outler - Unfortunately, this is a pretty conventional approach for binding in Java. Often instead of declaring your own listener type, and writing your own component, you'll be using pre-defined types from the JDK. But they work basically in this manner. I was going to write something similar in my answer, but I didn't have the energy. That's why I recommended a JavaFX UI, where a binding is essentially what you wrote in your question.
erickson