Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (button.id == Info_205_btn) {
- System.out.println("clicked");
- subject_name.setText("stanly");
- }
- @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();
- }
- button.setOnAction(event -> checkID(button));
- 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
- }
- for (int i = 0; i < buttonList.length; i++)
- button[i].setOnAction(event -> checkId((Button) event.getSource()));
- 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); }
- }
Add Comment
Please, Sign In to add comment