JavaFx - How to get button array index if value is all the same











up vote
0
down vote

favorite












I have an issue when I tried to create multiple buttons with the same value of "x". How can I fix this issue or perhaps get the value of the buttons and then delete the specific element in an array with the same index as my button without deleting other elements as it loops through the for loop?



Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array

for (int m=0; m <sizeOfIt; m++) {
delButton[m] = new Button("x");
}


for(int x = 0; x < delButton.length; x++) {
delButton[x].setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
// delete the element in the array with the same index as my button i clicked
}
});
}









share|improve this question
























  • Are you trying to remove the Button from the Scene or remove it from the Array?
    – Sedrick
    Nov 8 at 19:41















up vote
0
down vote

favorite












I have an issue when I tried to create multiple buttons with the same value of "x". How can I fix this issue or perhaps get the value of the buttons and then delete the specific element in an array with the same index as my button without deleting other elements as it loops through the for loop?



Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array

for (int m=0; m <sizeOfIt; m++) {
delButton[m] = new Button("x");
}


for(int x = 0; x < delButton.length; x++) {
delButton[x].setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
// delete the element in the array with the same index as my button i clicked
}
});
}









share|improve this question
























  • Are you trying to remove the Button from the Scene or remove it from the Array?
    – Sedrick
    Nov 8 at 19:41













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an issue when I tried to create multiple buttons with the same value of "x". How can I fix this issue or perhaps get the value of the buttons and then delete the specific element in an array with the same index as my button without deleting other elements as it loops through the for loop?



Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array

for (int m=0; m <sizeOfIt; m++) {
delButton[m] = new Button("x");
}


for(int x = 0; x < delButton.length; x++) {
delButton[x].setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
// delete the element in the array with the same index as my button i clicked
}
});
}









share|improve this question















I have an issue when I tried to create multiple buttons with the same value of "x". How can I fix this issue or perhaps get the value of the buttons and then delete the specific element in an array with the same index as my button without deleting other elements as it loops through the for loop?



Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array

for (int m=0; m <sizeOfIt; m++) {
delButton[m] = new Button("x");
}


for(int x = 0; x < delButton.length; x++) {
delButton[x].setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
// delete the element in the array with the same index as my button i clicked
}
});
}






java javafx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 18:48

























asked Nov 8 at 18:43









Michelle Ler

185




185












  • Are you trying to remove the Button from the Scene or remove it from the Array?
    – Sedrick
    Nov 8 at 19:41


















  • Are you trying to remove the Button from the Scene or remove it from the Array?
    – Sedrick
    Nov 8 at 19:41
















Are you trying to remove the Button from the Scene or remove it from the Array?
– Sedrick
Nov 8 at 19:41




Are you trying to remove the Button from the Scene or remove it from the Array?
– Sedrick
Nov 8 at 19:41












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You could handle this using the position of the button, it'll leave an empty box where the button was :



for(int x = 0; x < delButton.length; x++) {     
final index = x;
delButton[x].setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
delButton[index] = null;
}
});
}





share|improve this answer





















  • I am curious if this throws a NullPointerExecption? One would assume that if these buttons are in a Scene and set to null without removing them from the Scene, a NullPointerExecption should be thrown.
    – Sedrick
    Nov 8 at 19:56


















up vote
0
down vote













@azro example keeps up with the index to get a reference to the Buttons. This example uses actionEvent.getSource() to get a reference to the Buttons.



import java.util.Arrays;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application
{
int sizeOfIt = 10;

@Override
public void start(Stage primaryStage)
{
Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array
VBox vBox = new VBox();

for (int m = 0; m < sizeOfIt; m++) {
delButton[m] = new Button(m + "x");
delButton[m].setOnAction(actionEvent -> {
for (int i = 0; i < delButton.length; i++) {
if (delButton[i] != null && delButton[i].equals((Button) actionEvent.getSource())) {
vBox.getChildren().remove(delButton[i]);
delButton[i] = null;
System.out.println(Arrays.toString(delButton));
}
}
});
}

vBox.getChildren().addAll(delButton);
vBox.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);

StackPane root = new StackPane(vBox);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();

}

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





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214202%2fjavafx-how-to-get-button-array-index-if-value-is-all-the-same%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You could handle this using the position of the button, it'll leave an empty box where the button was :



    for(int x = 0; x < delButton.length; x++) {     
    final index = x;
    delButton[x].setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent event) {
    delButton[index] = null;
    }
    });
    }





    share|improve this answer





















    • I am curious if this throws a NullPointerExecption? One would assume that if these buttons are in a Scene and set to null without removing them from the Scene, a NullPointerExecption should be thrown.
      – Sedrick
      Nov 8 at 19:56















    up vote
    1
    down vote



    accepted










    You could handle this using the position of the button, it'll leave an empty box where the button was :



    for(int x = 0; x < delButton.length; x++) {     
    final index = x;
    delButton[x].setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent event) {
    delButton[index] = null;
    }
    });
    }





    share|improve this answer





















    • I am curious if this throws a NullPointerExecption? One would assume that if these buttons are in a Scene and set to null without removing them from the Scene, a NullPointerExecption should be thrown.
      – Sedrick
      Nov 8 at 19:56













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    You could handle this using the position of the button, it'll leave an empty box where the button was :



    for(int x = 0; x < delButton.length; x++) {     
    final index = x;
    delButton[x].setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent event) {
    delButton[index] = null;
    }
    });
    }





    share|improve this answer












    You could handle this using the position of the button, it'll leave an empty box where the button was :



    for(int x = 0; x < delButton.length; x++) {     
    final index = x;
    delButton[x].setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent event) {
    delButton[index] = null;
    }
    });
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 18:51









    azro

    9,95941438




    9,95941438












    • I am curious if this throws a NullPointerExecption? One would assume that if these buttons are in a Scene and set to null without removing them from the Scene, a NullPointerExecption should be thrown.
      – Sedrick
      Nov 8 at 19:56


















    • I am curious if this throws a NullPointerExecption? One would assume that if these buttons are in a Scene and set to null without removing them from the Scene, a NullPointerExecption should be thrown.
      – Sedrick
      Nov 8 at 19:56
















    I am curious if this throws a NullPointerExecption? One would assume that if these buttons are in a Scene and set to null without removing them from the Scene, a NullPointerExecption should be thrown.
    – Sedrick
    Nov 8 at 19:56




    I am curious if this throws a NullPointerExecption? One would assume that if these buttons are in a Scene and set to null without removing them from the Scene, a NullPointerExecption should be thrown.
    – Sedrick
    Nov 8 at 19:56












    up vote
    0
    down vote













    @azro example keeps up with the index to get a reference to the Buttons. This example uses actionEvent.getSource() to get a reference to the Buttons.



    import java.util.Arrays;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Control;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class Main extends Application
    {
    int sizeOfIt = 10;

    @Override
    public void start(Stage primaryStage)
    {
    Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array
    VBox vBox = new VBox();

    for (int m = 0; m < sizeOfIt; m++) {
    delButton[m] = new Button(m + "x");
    delButton[m].setOnAction(actionEvent -> {
    for (int i = 0; i < delButton.length; i++) {
    if (delButton[i] != null && delButton[i].equals((Button) actionEvent.getSource())) {
    vBox.getChildren().remove(delButton[i]);
    delButton[i] = null;
    System.out.println(Arrays.toString(delButton));
    }
    }
    });
    }

    vBox.getChildren().addAll(delButton);
    vBox.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);

    StackPane root = new StackPane(vBox);
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();

    }

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





    share|improve this answer

























      up vote
      0
      down vote













      @azro example keeps up with the index to get a reference to the Buttons. This example uses actionEvent.getSource() to get a reference to the Buttons.



      import java.util.Arrays;
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Control;
      import javafx.scene.layout.StackPane;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class Main extends Application
      {
      int sizeOfIt = 10;

      @Override
      public void start(Stage primaryStage)
      {
      Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array
      VBox vBox = new VBox();

      for (int m = 0; m < sizeOfIt; m++) {
      delButton[m] = new Button(m + "x");
      delButton[m].setOnAction(actionEvent -> {
      for (int i = 0; i < delButton.length; i++) {
      if (delButton[i] != null && delButton[i].equals((Button) actionEvent.getSource())) {
      vBox.getChildren().remove(delButton[i]);
      delButton[i] = null;
      System.out.println(Arrays.toString(delButton));
      }
      }
      });
      }

      vBox.getChildren().addAll(delButton);
      vBox.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);

      StackPane root = new StackPane(vBox);
      Scene scene = new Scene(root);
      primaryStage.setScene(scene);
      primaryStage.show();

      }

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





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        @azro example keeps up with the index to get a reference to the Buttons. This example uses actionEvent.getSource() to get a reference to the Buttons.



        import java.util.Arrays;
        import javafx.application.Application;
        import javafx.scene.Scene;
        import javafx.scene.control.Button;
        import javafx.scene.control.Control;
        import javafx.scene.layout.StackPane;
        import javafx.scene.layout.VBox;
        import javafx.stage.Stage;

        public class Main extends Application
        {
        int sizeOfIt = 10;

        @Override
        public void start(Stage primaryStage)
        {
        Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array
        VBox vBox = new VBox();

        for (int m = 0; m < sizeOfIt; m++) {
        delButton[m] = new Button(m + "x");
        delButton[m].setOnAction(actionEvent -> {
        for (int i = 0; i < delButton.length; i++) {
        if (delButton[i] != null && delButton[i].equals((Button) actionEvent.getSource())) {
        vBox.getChildren().remove(delButton[i]);
        delButton[i] = null;
        System.out.println(Arrays.toString(delButton));
        }
        }
        });
        }

        vBox.getChildren().addAll(delButton);
        vBox.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);

        StackPane root = new StackPane(vBox);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

        }

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





        share|improve this answer












        @azro example keeps up with the index to get a reference to the Buttons. This example uses actionEvent.getSource() to get a reference to the Buttons.



        import java.util.Arrays;
        import javafx.application.Application;
        import javafx.scene.Scene;
        import javafx.scene.control.Button;
        import javafx.scene.control.Control;
        import javafx.scene.layout.StackPane;
        import javafx.scene.layout.VBox;
        import javafx.stage.Stage;

        public class Main extends Application
        {
        int sizeOfIt = 10;

        @Override
        public void start(Stage primaryStage)
        {
        Button delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array
        VBox vBox = new VBox();

        for (int m = 0; m < sizeOfIt; m++) {
        delButton[m] = new Button(m + "x");
        delButton[m].setOnAction(actionEvent -> {
        for (int i = 0; i < delButton.length; i++) {
        if (delButton[i] != null && delButton[i].equals((Button) actionEvent.getSource())) {
        vBox.getChildren().remove(delButton[i]);
        delButton[i] = null;
        System.out.println(Arrays.toString(delButton));
        }
        }
        });
        }

        vBox.getChildren().addAll(delButton);
        vBox.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);

        StackPane root = new StackPane(vBox);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

        }

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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 19:59









        Sedrick

        5,87531838




        5,87531838






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214202%2fjavafx-how-to-get-button-array-index-if-value-is-all-the-same%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini