0

I am trying to change the text of a Button in a GridPane when the Button is clicked. I have successfully managed to do so. but, I am getting this exception.

I have this function that changes the text.

public void clicker(Button b) {
    if (b.getText().equals(" ")) {
        if (turn == 0) {
            b.setText("X");
            b.setFont(Font.font("Times", FontWeight.EXTRA_BOLD,
                    60));
            turn = 1;
        } else if (turn == 1) {
            Button bNew = new Button("Y");
            bNew.setFont(Font.font("Times", FontWeight.EXTRA_BOLD, 60));
            b.setText("Y");
            b.setFont(Font.font("Times", FontWeight.EXTRA_BOLD,
                    60));
            turn = 0;
        }
    }
}

this is the pane set up and event handler where the errors are happening.

GridPane pane = new GridPane();


    b1.setMinSize(100, 100);
    b2.setMinSize(100, 100);
    b3.setMinSize(100, 100);
    b4.setMinSize(100, 100);
    b5.setMinSize(100, 100);
    b6.setMinSize(100, 100);
    b7.setMinSize(100, 100);
    b8.setMinSize(100, 100);
    b9.setMinSize(100, 100);




    GridPane.setConstraints(b1, 0, 0);
    GridPane.setConstraints(b2, 1, 0);
    GridPane.setConstraints(b3, 2, 0);
    GridPane.setConstraints(b4, 0, 1);
    GridPane.setConstraints(b5, 1, 1);
    GridPane.setConstraints(b6, 2, 1);
    GridPane.setConstraints(b7, 0, 2);
    GridPane.setConstraints(b8, 1, 2);
    GridPane.setConstraints(b9, 2, 2);


    pane.getChildren().addAll(b1, b2, b3, b4, b5, b6, b7, b8, b9);

    b1.setOnAction((event) -> {
        clicker(b1);
        pane.getChildren().add(b1);
    });

    b2.setOnAction((event) -> {
        clicker(b2);
        pane.getChildren().add(b2);
    });

any help would be appreciated. thanks

joezanini
  • 13
  • 1
  • 6
  • 1
    The error should be pretty self-explanatory: You're adding the `b1` or the `b2` button to the same parent twice which is not allowed. (Even adding the button to some other parent would remove it from the old parent.) And I don't know what you're creating `bNew` for. – fabian Mar 01 '18 at 23:57
  • 1
    Why do you add the button, you've already added it – Vivick Mar 01 '18 at 23:57
  • without adding the button again the text never changes – joezanini Mar 01 '18 at 23:59
  • I am making a tic tac toe game – joezanini Mar 02 '18 at 00:00
  • Do you use the default text for the buttons or do you assign a single space character as value for the `Button.text` property? (This would be required because of your `equals` check.) – fabian Mar 02 '18 at 00:03
  • Button b1 = new Button(" "); – joezanini Mar 02 '18 at 00:06
  • And the value of `turn`? Are you sure it's always `0` or `1`? – fabian Mar 02 '18 at 00:08
  • yes, I set it to 0 upon initialization. – joezanini Mar 02 '18 at 00:14
  • Hmm... Works flawlessly, if I increase the button size to 200x200 (in addition to removing the lines where you readd the buttons to the `GridPane`). Probably a issue with the text being too large... – fabian Mar 02 '18 at 00:35
  • Thank you so much!! i switched the size of the text to 30 and it worked. – joezanini Mar 02 '18 at 00:37

0 Answers0