Answer the question
In order to leave comments, you need to log in
How to create a "change" event for a variable in Java?
There is a variable with type double. The value of this variable is calculated in an infinite loop and it is constantly changing. This value should be displayed on the form in JTextField. Now I have created a simple class that inherits from JTextField:
public class ChartTabTextField extends JTextField {
private double factor = 1.0;
private String format = "%d";
public ChartTabTextField(double d, String format, double factor) {
this.factor = factor;
this.format = format;
setText(d);
}
public void setText(double d) {
if (format.equals("%d")) {
setText(String.format(format, (int) (d * factor)));
} else {
setText(String.format(format, d * factor));
}
}
}
ChartTabTextField jtf = new ChartTabTextField(ve.pot.params.amp, "%.1f", 0.001);
jPanel1.add(jtf);
Answer the question
In order to leave comments, you need to log in
See, there are 2 main ways an object interacts with another object.
1 object gets another object,
2 object takes what it needs.
If you don't want to do setText, then JTextField itself should take the value.
You should move away from the primitive to value boxing, or better create a class
that will control the change, and JTextField can act
as a listener through some interface
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question