0

I am trying to display 9 buttons using javafx.

public class Main extends Application 
{
    public void start(Stage primaryStage) throws Exception 
    {

        Button[] button = new Button[10];
        Pane pane = new Pane(); 

        for( int i=0; i < 9; i++)
        {
            button[i].setText("hi");
            button[i].setText("hi");
            button[i].setLayoutX(i*10);

            System.out.println(button[i].getText());
            pane.getChildren().addAll(button[i]);                   
        }

        Scene scene = new Scene(pane); 
        primaryStage.setScene(scene);
        primaryStage.show();       


}
1
  • 1
    How should the buttons be orientated? What should the Button's say on them, can you give us some more details, please? Commented Sep 23, 2017 at 20:31

1 Answer 1

2

You create an array able to store Button element, but you never create the Button itself

So you have to do :

for( int i=0; i < 9; i++){
    button[i] = new Button();     // <-- here
    button[i].setText("hi");      // you have twice this line
    button[i].setLayoutX(i*10);

    System.out.println(button[i].getText());
    pane.getChildren().addAll(button[i]);                   
}

Also, if for other part you don't need to retrieve the button later, you don't need so store them and use an array, this will work :

for( int i=0; i < 9; i++){
    Button btn = new Button();     // <-- here
    btn.setText("hi");
    btn.setLayoutX(i*10);

    System.out.println(btn.getText());
    pane.getChildren().addAll(btn);                   
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.