Stanly Medjo
Stanly Medjo

Reputation: 95

getting the Id of a button in JavaFX

I created about 10 buttons in javafx each of them onclick is suppose to change the label field in a certain way. My problem is that I don't want to create 10 different methods for each label I will like to use one method then test the id of the button if correct I preform what I want

example of what I am asking

   if (button.id == Info_205_btn) {
        System.out.println("clicked");
     subject_name.setText("stanly");
    } 

This is an update after @math answer

Here is the code I did

    @FXML
    private void chooseSubject() {
  for (int i = 0; i < buttonInfo.length; i++) {
            buttonInfo[i] = new Button("Info"+i);
            buttonInfo[i].setId("Info"+i);
            int finalI = i;
            buttonInfo[i].setOnAction(event -> checkID(buttonInfo[finalI]));
        }
    }


    @FXML
    private void checkID(Button button){
                System.out.println("running");

    if (button.getId().equals("Info0")) {
        System.out.println("clicked");
       subject_name.setText("stanly");
    }
    else if (button.getId().equals("Info1")) {
        System.out.println("clicked");
       subject_name.setText("stanly1");
    }
}


    @Override
    public void initialize(URL url, ResourceBundle rb) {
         chooseSubject();
    }    

also on click I placed the method chooseSubject in FXML controller

Upvotes: 1

Views: 11627

Answers (1)

Matt
Matt

Reputation: 3197

For all of your buttons I think you will still need to put a .setOnAction but you can have them all point to the same function

button.setOnAction(event -> checkID(button));

and from that function check the id

private void checkID(Button button){
    if (button.getId().equals("Info_205_btn")) {
        System.out.println("clicked");
        button.setText("stanly");
    }
    else if (button.getId().equals("Info_206_btn")) {
        System.out.println("clicked");
        button.setText("stanly");
    }
    //So on
}

Also if you put all of your buttons into a list or if they are already in a list you can iterate though the list and do the .setOnAction that way

for (int i = 0; i < buttonList.length; i++) 
    button[i].setOnAction(event -> checkId((Button) event.getSource()));

Here is a test program I just wrote to give you an example

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        Button[] buttonList = new Button[10];

        for (int i = 0; i < buttonList.length; i++) {
            buttonList[i] = new Button("Button "+i);
            buttonList[i].setId("Button"+i);
            buttonList[i].setOnAction(event -> checkId((Button) event.getSource()));
        }

        VBox root = new VBox();
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(buttonList);
        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setWidth(200);
        stage.setScene(scene);
        stage.show();
    }

    private void checkId(Button button) {
        for (int i = 0; i <= 10; i++) {
            if(button.getId().equals("Button" + i))
                if(!button.getText().equals("Button " + i + " Clicked"))
                    button.setText("Button " + i + " Clicked");
                else
                    button.setText("Button " + i);
        }
    }

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

Edit: Got carried away

Upvotes: 1

Related Questions