To reproduce run the app and push 'Tab' several times.
You will notice that when focus goes from the rectangle to the text
the rectangle will fire change event.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* @author alexandr_kirov
*/
public class NewClass extends Application {
@Override
public void start(Stage stage) throws Exception {
Text text = new Text("text");
text.setFocusTraversable(true);
Rectangle r = new Rectangle(100, 100, 100, 100);
r.setFill(Color.ALICEBLUE);
r.setFocusTraversable(true);
text.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println("Text focused.");
}
});
r.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println("Rectangle focused.");
}
});
VBox vb = new VBox();
vb.getChildren().addAll(r, text);
Scene scene = new Scene(vb, 300, 300);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You will notice that when focus goes from the rectangle to the text
the rectangle will fire change event.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* @author alexandr_kirov
*/
public class NewClass extends Application {
@Override
public void start(Stage stage) throws Exception {
Text text = new Text("text");
text.setFocusTraversable(true);
Rectangle r = new Rectangle(100, 100, 100, 100);
r.setFill(Color.ALICEBLUE);
r.setFocusTraversable(true);
text.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println("Text focused.");
}
});
r.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println("Rectangle focused.");
}
});
VBox vb = new VBox();
vb.getChildren().addAll(r, text);
Scene scene = new Scene(vb, 300, 300);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}