0

I am trying to put multiple textfields in a circle in JavaFX. I could add a field in the centre using StackPane as explained in the below-mentioned post but unable to add multiple textfields. I tried using different panes for that but it didn't work.

Added the code that doesn't work.I want to add two text fields at any place inside a circle. Using gridpane for it didn't work. Moreover, I want to create x number of circle dynamically at any place in a gridpane and add multiple text fields to the circle, is it possible to do that using JavaFX?

Hope I am able to explain the problem statement correctly. Any response is appreciated :)

    @Override
public void start(Stage arg0) throws Exception {
    arg0.setTitle("Text Boxes In circle");
    arg0.setMaxWidth(500);

    Circle circle = createCircle(); // This function is to form a circle.
    Text text = new Text("42");
    Text text1 = new Text("36");
    text.setBoundsType(TextBoundsType.VISUAL);
    text1.setBoundsType(TextBoundsType.VISUAL);
    GridPane box = new GridPane();
    // box.setConstraints(text, 2, 0); commented this out to check if it was not
    // causing problem but still didn't work
    // box.setConstraints(text1, 2, 1);
    // box.setAlignment(Pos.CENTER); Even used this to center the gridPane didn't
    // work either.
    StackPane stack = new StackPane();
    box.getChildren().addAll(text, text1);
    stack.getChildren().addAll(box, circle);

    Scene scene = new Scene(stack);
    arg0.setScene(scene);
    arg0.show();


}

public static void main(String[] args) {
    launch(args);
}

private static Circle createCircle() {
    final Circle circle = new Circle(100);
    circle.setStroke(Color.FORESTGREEN);
    circle.setStrokeWidth(10);
    circle.setStrokeType(StrokeType.INSIDE);
    circle.setFill(Color.AZURE);
    return circle;
}

how to put a text into a circle object to display it from circle's center?

  • Add a `VBox` to the `StackPane`. Then add your `TextFields` to the `VBox`. – SedJ601 Sep 19 '18 at 16:19
  • Hi, I tried doing that. Below is the code snippet for it but it didn't work. I am still getting the numbers out of the circle at the top left. Circle circle = createCircle(); Text text = new Text("42"); Text text1 = new Text("36"); text.setBoundsType(TextBoundsType.VISUAL); text1.setBoundsType(TextBoundsType.VISUAL); VBox box = new VBox(10); StackPane stack = new StackPane(); stack.getChildren().addAll(box,circle); box.getChildren().addAll(text,text1); Scene scene = new Scene(stack); using the funciton from the post. – Shivam Aggarwal Sep 19 '18 at 16:35
  • Try GridPane instead of VBox. – SedJ601 Sep 19 '18 at 16:42
  • It still didn't work. Apparently, GridPane is not binding with StackPane and text is still displayed on the top left. :/ – Shivam Aggarwal Sep 19 '18 at 17:13
  • 2
    Please create a [mcve] that demonstrates the problem. – Slaw Sep 19 '18 at 19:20
  • Hi, tried to explain the problem in more detail. Hope I have been able to follow the points mentioned in the link. – Shivam Aggarwal Sep 20 '18 at 17:12

0 Answers0