Hello,
I am trying to place a circle at the top left corner of the node that has the focus. The following code works, if I change the focus using mouse or the tab key. If I resize the stage, by making it smaller, the circle is not positioned correctly. The coordinate conversions return the values as if the stage was not resized. That is, adding the change listeners to the width and height properties of the stage does not make any difference.
Can someone point me in the right direction as to what I am missing?
Thanks
Kishori
// CoordinateConversion.java
package test;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class CoordinateConversion extends Application {
// An instance variable to store the reference of the circle
private Circle marker;
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage stage) {
TextField fName = new TextField();
TextField lName = new TextField();
TextField salary = new TextField();
// The Circle node is unmanaged
marker = new Circle(5);
marker.setManaged(false);
marker.setFill(Color.RED);
marker.setMouseTransparent(true);
HBox hb1 = new HBox();
HBox hb2 = new HBox();
HBox hb3 = new HBox();
hb1.getChildren().addAll(new Label("First Name:"), fName);
hb2.getChildren().addAll(new Label("Last Name:"), lName);
hb3.getChildren().addAll(new Label("Salary:"), salary);
VBox root = new VBox();
root.getChildren().addAll(hb1, hb2, hb3, marker);
final Scene scene = new Scene(root);
// Add a focus change listener to the scene
scene.focusOwnerProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue prop, Object oldNode, Object newNode) {
layoutMarker(scene.getFocusOwner());
}
});
stage.widthProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observableValue, Object t, Object t1) {
layoutMarker(scene.getFocusOwner());
}
});
stage.heightProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observableValue, Object t, Object t1) {
layoutMarker(scene.getFocusOwner());
}
});
stage.setScene(scene);
stage.setTitle("Coordinate Space Transformation");
stage.show();
}
public void layoutMarker(Node newNode) {
if (newNode == null) {
return;
}
double nodeMinX = newNode.getLayoutBounds().getMinX();
double nodeMinY = newNode.getLayoutBounds().getMinY();
Point2D nodeInScene = newNode.localToScene(nodeMinX, nodeMinY);
Point2D nodeInMarkerLocal = marker.sceneToLocal(nodeInScene);
Point2D nodeInmarkerParent = marker.localToParent(nodeInMarkerLocal);
marker.relocate(nodeInmarkerParent.getX() + marker.getLayoutBounds().getMinX(),
nodeInmarkerParent.getY() + marker.getLayoutBounds().getMinY());
}
}
Edited by: Kishori Sharan on Dec 13, 2012 7:49 AM