0

I'm building a rock, scissors, paper application as a college homework. I should use radio-buttons and since it's a game, it should allows one selected button at a time.

I tried to create ToggleGroups and set one of the buttons as selected by default, but it's not working! When I run the application, it still allows me to choose more than one button :( What am I missing?

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("../view/QuilometrosPorLitroView.fxml"));
        primaryStage.setTitle("Pedra, Papel, Tesoura");
        primaryStage.setScene(new Scene(root, 428, 336));
        primaryStage.setResizable(false);
        primaryStage.show();
        Controller.selectButtonDefault();
    }


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

public class Controller {

    @FXML
    public static
    RadioButton tesoura = new RadioButton();
    @FXML
    public static
    RadioButton pedra = new RadioButton();
    @FXML
    public static
    RadioButton papel = new RadioButton();

    public static void selectButtonDefault() {
        ToggleGroup group = new ToggleGroup();

        tesoura.setToggleGroup(group);
        tesoura.setSelected(true);
        pedra.setToggleGroup(group);
        papel.setToggleGroup(group);

    }
  • First you should know how to create the controller class. If you using the Scenebuilder you can show the Sample skelection. Than you will See the different from @FXML and how you create the things with "new" – Raw Jun 15 '19 at 22:14
  • You cannot inject nodes to `static` fields. You probably try to fix the issue of the fields being `null` by initializing them yourself, but those `Node`s are not part of any scene. – fabian Jun 16 '19 at 01:48

1 Answers1

0

This

@FXML public static RadioButton tesoura = new RadioButton(); 
@FXML public static RadioButton pedra = new RadioButton(); 
@FXML public static RadioButton papel = new RadioButton();

Should be changed into

@FXML private RadioButton tesoura;
@FXML private RadioButton pedra; 
@FXML private RadioButton papel;
Raw
  • 461
  • 7
  • 15
  • I tried, but I got a Null Pointer. ```Caused by: java.lang.NullPointerException at controller.Controller.selectButtonDefault(Controller.java:23) at main.Main.start(Main.java:22)``` – Lais Bento Jun 16 '19 at 00:10
  • Show us the fxml file. I think you forgot to set the IDs. – Raw Jun 16 '19 at 02:12