Java Reference
In-Depth Information
public var text:String = getJTextArea().getText() on replace {
getJTextArea().setText(text);
};
Next, to update the JavaFX text variable, when the
JTextArea text
attribute
changes, we need to install a JavaBeans property change listener on the
JTextArea
.
init {
var textArea = getJTextArea();
textArea.addPropertyChangeListener(
PropertyChangeListener {
public override function propertyChange(
ev: PropertyChangeEvent) : Void {
var property = ev.getPropertyName();
if(property == "text") {
text = textArea.getText();
}
...
Now, whenever the
JTextArea text
changes, a JavaBeans property change
event will be fired, and the JavaFX corresponding instance variable can be set.
However, we have a problem. If the JavaFX program changes the JavaFX
text
instance variable, it will in turn call the
JTextArea.setText()
method, which
causes the JavaBeans property change event to fire, which in turn sets the JavaFX
text
instance variable. We are stuck in an infinite circle and eventually our pro-
gram will crash.
To overcome this, we need to add a flag indicating this condition to our JavaFX
class. This indicates that the JavaFX
text
variable change caused the property
change event to fire, so there is no need for the JavaBeans property change lis-
tener to update the companion JavaFX variable. We added the private Boolean
variable,
inChange
.
var inChange = false;
public var text:String = getJTextArea().getText()
on replace {
if(not text.equals(getJTextArea().getText())){
try {
inChange = true;
getJTextArea().setText(text);
}finally {
inChange = false;
}
}
};
...
Search WWH ::
Custom Search