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
}
});
}
java javafx
add a comment |
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
}
});
}
java javafx
Are you trying to remove theButton
from theScene
or remove it from theArray
?
– Sedrick
Nov 8 at 19:41
add a comment |
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
}
});
}
java javafx
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
java javafx
edited Nov 8 at 18:48
asked Nov 8 at 18:43
Michelle Ler
185
185
Are you trying to remove theButton
from theScene
or remove it from theArray
?
– Sedrick
Nov 8 at 19:41
add a comment |
Are you trying to remove theButton
from theScene
or remove it from theArray
?
– 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
add a comment |
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;
}
});
}
I am curious if this throws aNullPointerExecption
? One would assume that if these buttons are in aScene
and set to null without removing them from theScene
, aNullPointerExecption
should be thrown.
– Sedrick
Nov 8 at 19:56
add a comment |
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);
}
}
add a comment |
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;
}
});
}
I am curious if this throws aNullPointerExecption
? One would assume that if these buttons are in aScene
and set to null without removing them from theScene
, aNullPointerExecption
should be thrown.
– Sedrick
Nov 8 at 19:56
add a comment |
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;
}
});
}
I am curious if this throws aNullPointerExecption
? One would assume that if these buttons are in aScene
and set to null without removing them from theScene
, aNullPointerExecption
should be thrown.
– Sedrick
Nov 8 at 19:56
add a comment |
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;
}
});
}
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;
}
});
}
answered Nov 8 at 18:51
azro
9,95941438
9,95941438
I am curious if this throws aNullPointerExecption
? One would assume that if these buttons are in aScene
and set to null without removing them from theScene
, aNullPointerExecption
should be thrown.
– Sedrick
Nov 8 at 19:56
add a comment |
I am curious if this throws aNullPointerExecption
? One would assume that if these buttons are in aScene
and set to null without removing them from theScene
, aNullPointerExecption
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
add a comment |
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);
}
}
add a comment |
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);
}
}
add a comment |
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);
}
}
@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);
}
}
answered Nov 8 at 19:59
Sedrick
5,87531838
5,87531838
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Are you trying to remove the
Button
from theScene
or remove it from theArray
?– Sedrick
Nov 8 at 19:41