0

I currently have a program where you can press one "stack button" and create 9 stacks (which are just buttons). However, I want it to where each button has it's own identity, so if I click on a specific one, and it will take me to a specific window for each.

Here is my code:

private void createButtonActivate(){
    System.out.println("A stack has been created");
    stackCount++;
    newStackButton = new Button((Integer.toString(stackCount)));
    newStackButton.setOnAction(actionEvent -> {
        methods.openNoteCard();
    });

    root.getChildren().add(newStackButton);
}

Here is what is called to create the Buttons:

private void createStack(){
    stackCreated = true;

    if (stackCount < 3 ) {
        createButtonActivate();
        buttonXPos = buttonXPos + 150;
    }
    else if (stackCount >= 3 && stackCount < 6){
        if (stackCount == 3){
            buttonXPos = 50;
            buttonYPos = buttonYPos + 150;
        }
        createButtonActivate();
        buttonXPos = buttonXPos + 150;
       // buttonXPos =
    }
    else if (stackCount >= 6 && stackCount < 9){
        if (stackCount == 6){
            buttonXPos = 50;
            buttonYPos = buttonYPos + 150;
        }
        createButtonActivate();
        buttonXPos = buttonXPos + 150;
    }

}

I hope I didn't provide too much code and I actually explained my problem thoroughly haha anyway, thank you for any help!

5
  • "I want it to where each button has it's own identity, so if I click on a specific one, and it will take me to a specific window for each" - it sounds to me like you're mixing up concepts. You want the buttons to have individual actions, but that's not the same thing as needing them to be referred to be individual names. Without a better description of what you want to achieve, it's hard to give any more help. Possibly it's just a matter of adding a parameter to the createButtonActivate method to specify which window the button should associate with? Commented Oct 18, 2018 at 14:54
  • I may have explained it poorly but looks like someone understood and helped me out. Thank you though! Commented Oct 18, 2018 at 15:19
  • Unrelated Note: Are you sure a StackPane is the best layout option here? You may be able to save a lot of work and trouble by using a layout that arranges the buttons for you (instead of trying to lay them out at precise coordinates yourself). Commented Oct 18, 2018 at 18:04
  • @Zephyr Absolutely not sure haha I am decently new to it all. What would you recommend? If you have links, I love reading! Commented Oct 18, 2018 at 18:26
  • @DannonGilbert - it depends on what you want your final layout to look like. You could use a FlowPane if you want to add multiple nodes in a row and have them flow to the next line. Or a GridPane if you want them evenly spaced in precise row/column locations. There are a lot of options. Google for JavaFX layouts and take a look :) Commented Oct 18, 2018 at 18:44

1 Answer 1

1

If you want to make buttons with unique logic when they are clicked, you can actually add a parameter to your openNoteCard method.

For example:

void openNoteCard(int stackId) {
    switch (stackId) {
        case 0: //logic for the first button
        case 1: //logic for the second button
        // and so on...
    }
}

So, in your createButtonActive method, it will look like this:

private void createButtonActivate(){
    System.out.println("A stack has been created");
    stackCount++;
    newStackButton = new Button((Integer.toString(stackCount)));
    newStackButton.setOnAction(actionEvent -> {
        methods.openNoteCard(stackCount); // Passing a parameter here
    });

    root.getChildren().add(newStackButton);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Whoa!! Thank you. I can't wait to implement this. That helps a lot.

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.