pwnster1357
pwnster1357

Reputation: 35

how do i differentiate between two different JButtons in actionPerformed?

so i have my JButtons

public static JButton textYes = new JButton("Yes");
public static JButton textNo = new JButton("No");

my menus

public static String choiceReroll = "";
public static String menu = "";

and my main() method

public static void main(String[] args) throws IOException {
    Greed gui = new Greed();
    gui.launchFrame();
    redirectSystemStreams();

    Container contentPane = f.getContentPane();
    contentPane.add(new Greed());

    Scanner is = new Scanner(System.in);
    System.out.println("Welcome to Greed...");
        do {
    System.out.println("Would you like to play? (yes/no)");
    menu = is.next();
            switch (menu) {
                case "yes":
                    jTextArea1.setText(null);
                    diceOne = 0;
                    diceTwo = 0;
                    diceThree = 0;
                    diceFour = 0;
                    diceFive = 0;
                    System.out.println("Rolling...");
                    Game();

                    break;
                case "no":
                    System.out.println("Goodbye...");
                    System.exit(0);

                    break;
                default:
                    invalidInput();

                    break;
            }
        } while (!"yes".equals(menu) || !"no".equals(menu));
}

then i have my actionListeners

textYes.addActionListener(this);
textNo.addActionListener(this);

how do i make it so that when i click on one button or the other, it will enter text where it requests user input at menu = is.next();

i also want it to just enter text, depending on which button it clicked on. and have it enter the text, no matter what variable is asking for input

for example:

menu = is.next();

vs.

choiceReroll = is.next();

EDIT: more info...

and i also have my performedAction() method

public void actionPerformed(ActionEvent e) {
    jTextArea1.setText(null);
    if (box1.isSelected()) {
        System.out.println("1 is selected");
        willRerollDiceOne = true;
    }
    else {
        System.out.println("1 not selected");
        willRerollDiceOne = false;
    }
    if (box2.isSelected()) {
        System.out.println("2 is selected");
        willRerollDiceTwo = true;
    }
    else {
        System.out.println("2 not selected");
        willRerollDiceTwo = false;
    }
    if (box3.isSelected()) {
        System.out.println("3 is selected");
        willRerollDiceThree = true;
    }
    else {
        System.out.println("3 not selected");
        willRerollDiceThree = false;
    }
    if (box4.isSelected()) {
        System.out.println("4 is selected");
        willRerollDiceFour = true;
    }
    else {
        System.out.println("4 not selected");
        willRerollDiceFour = false;
    }
    if (box5.isSelected()) {
        System.out.println("5 is selected");
        willRerollDiceFive = true;
    }
    else {
        System.out.println("5 not selected");
        willRerollDiceFive = false;
    }

EDIT: i added the actionPerformed for the buttons, but am only going to show one for now

    if (area == "menu") {
        if(e.getSource() == textYes){
            menu = "yes";
        }
        if(e.getSource() == textNo){
            menu = "no";
        }
    }

but when i click the button, it doesn't update, why? how do i fix this?

EDIT: added a "test" printout inside the if statement

    if ("menu".equals(area)) {
            if(e.getSource() == textYes){
                menu = "yes";
                System.out.println("test");
            }
            if(e.getSource() == textNo){
                menu = "no";
            }

prints "test" every time i click the yes button

EDIT: This is the rest of the method:

    public static void main(String[] args) throws IOException {
        Greed gui = new Greed();
        gui.launchFrame();
        redirectSystemStreams();

        Container contentPane = f.getContentPane();
        contentPane.add(new Greed());

        Scanner is = new Scanner(System.in);
        System.out.println("Welcome to Greed...");
            do {
        System.out.println("Would you like to play? (yes/no)");
        area = "menu";
        menu = is.next();
                switch (menu) {
                    case "yes":
                        jTextArea1.setText(null);
                        diceOne = 0;
                        diceTwo = 0;
                        diceThree = 0;
                        diceFour = 0;
                        diceFive = 0;
                        System.out.println("Rolling...");
                        Game();

                        break;
                    case "no":
                        System.out.println("Goodbye...");
                        System.exit(0);

                        break;
                    default:
                        invalidInput();

                        break;
                }
            } while (!"yes".equals(menu) || !"no".equals(menu));
        area = "";
    }

    public static void Game() throws IOException {
        rollDiceOne();
        rollDiceTwo();
        rollDiceThree();
        rollDiceFour();
        rollDiceFive();

        //displayDice();
        displayDiceValues();
        f.validate();
        f.repaint();

        choiceRerollDice();
    }

    public static void choiceRerollDice() {
        Scanner is = new Scanner(System.in);
            do {
                if (!canRerollDiceOne && !canRerollDiceTwo && !canRerollDiceThree && !canRerollDiceFour && !canRerollDiceFive) {
                System.out.println("Sorry, but you may not reroll any more dice...");
                displayDiceValues();
                System.exit(0);
            }
            else {
        System.out.println("Would you like to reroll any (more) dice? (yes/no)");
            choiceReroll = is.next();
                switch (choiceReroll) {
                    case "yes":
                        rerollDice();
                        break;
                    case "no":
                        //endTurn();
                        displayDiceValues();
                        f.repaint();
                        //calculatePlayer1Score();
                        //System.out.println("Thank you for playing!");
                        //System.out.println("Goodbye!");
                        System.exit(0);
                    default:
                        invalidInput();
                    }
                }
            } while (!"yes".equals(choiceReroll) || !"no".equals(choiceReroll));
    }

    public static void rerollDice() {
        Scanner is = new Scanner(System.in);
        System.out.println("Which dice would you like to reroll? (Click the box under the dice!)");
        rollSel = is.next();
        switch (rollSel) {
            case "roll":
            if (willRerollDiceOne) {
                if (canRerollDiceOne) {
                    diceOne = 0;
                    rollDiceOne();
                    canRerollDiceOne = false;
                    box1.setEnabled(false);
                }
                else {
                    System.out.println("error");
                }
            }

            else {
            }
            if (willRerollDiceTwo) {
                if (canRerollDiceTwo) {
                diceTwo = 0;
                rollDiceTwo();
                canRerollDiceTwo = false;
                box2.setEnabled(false);
                }
                else {
                    System.out.println("error");
                }
            }
            else {
            }
            if (willRerollDiceThree) {
                if (canRerollDiceThree) {
                diceThree = 0;
                rollDiceThree();
                canRerollDiceThree = false;
                box3.setEnabled(false);
                }
            }
            else {
            }
            if (willRerollDiceFour) {
                if (canRerollDiceFour) {
                diceFour = 0;
                rollDiceFour();
                canRerollDiceFour = false;
                box4.setEnabled(false);
                }
            }
            else {
            }
            if (willRerollDiceFive) {
                if (canRerollDiceFive) {
                diceFive = 0;
                rollDiceFive();
                canRerollDiceFive = false;
                box5.setEnabled(false);
                }
            }
            else {
            }
            box1.setSelected(false);
            box2.setSelected(false);
            box3.setSelected(false);
            box4.setSelected(false);
            box5.setSelected(false);
            f.validate();
            f.repaint();
                break;
            default:
                System.out.println("Error...");
        }

Upvotes: 1

Views: 6006

Answers (5)

Alexis C.
Alexis C.

Reputation: 93902

In your actionPerformed method just do :

public void actionPerformed(ActionEvent e) { 
    if(e.getSource() == textYes){
      //perform action when textYes clicked
    }
    if(e.getSource() == textNo){
      //perform action when textNo clicked
    }
}

Instead of this :

Scanner is = new Scanner(System.in);
        System.out.println("Welcome to Greed...");
            do {
        System.out.println("Would you like to play? (yes/no)");
        area = "menu";
        menu = is.next();
                switch (menu) {
                    case "yes":
                        jTextArea1.setText(null);
                        diceOne = 0;
                        diceTwo = 0;
                        diceThree = 0;
                        diceFour = 0;
                        diceFive = 0;
                        System.out.println("Rolling...");
                        Game();

                        break;
                    case "no":
                        System.out.println("Goodbye...");
                        System.exit(0);

                        break;
                    default:
                        invalidInput();

                        break;
                }
            } while (!"yes".equals(menu) || !"no".equals(menu));
        area = "";

You could create a method that will take the input as a parameter.

public void start(String menu){
                switch (menu) {
                    case "yes":
                        jTextArea1.setText(null);
                        diceOne = 0;
                        diceTwo = 0;
                        diceThree = 0;
                        diceFour = 0;
                        diceFive = 0;
                        System.out.println("Rolling...");
                        Game();

                        break;
                    case "no":
                        System.out.println("Goodbye...");
                        System.exit(0);

                        break;
                    default:
                        invalidInput();

                        break;
                }
}

Then in the action performed, just do :

if(e.getSource() == textYes){
            start("yes");
        }
        if(e.getSource() == textNo){
            start("no");
        }

Upvotes: 4

Andreas Fester
Andreas Fester

Reputation: 36649

how do i differentiate between two different JButtons in actionPerformed? i have my actionListeners

textYes.addActionListener(this);
textNo.addActionListener(this);

The cleanest approach is to use separate listeners. You can do this with an anonymous inner class, like

textYes.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) { 
        // here you can do any necessary actions for the "Yes" button,
        // like calling a specific method of the outer class which handles the event
    }
});

textNo.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) { 
        // here you can do any necessary actions for the "No" button,
        // like calling a specific method of the outer class which handles the event
    }
});

If you still want to use one action listener only, the getSource() method from ActionEvent allows you to access the source of the event, as @ZouZou already mentioned.

See also How do you add an ActionListener onto a JButton in Java.

Upvotes: 4

darijan
darijan

Reputation: 9795

Assign different ActionListener to each field. I strongly advise you to do that, and not extend the Container that has those fields with ActionListener and pass it as this. It preserves encapsulation, it enforces separation of concerns and its a matter of clean, understandable code.

So, each button should have ActionListener added like this:

textYes.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(Action event e) {
        //logic
    }
});

Upvotes: 1

Alpesh Gediya
Alpesh Gediya

Reputation: 3794

getSource() is specified by the EventObject class that ActionEvent is a child of (via java.awt.AWTEvent). This gives you a reference to the object that the event came from

Or either set command for each JButton using setActionComman(String command) and then fetch relevant action Command Using getActionCommand.

String cmd = event.getActionCommand();

Upvotes: 1

LoganMzz
LoganMzz

Reputation: 1619

From the ActionEvent, you can get the action command. So, just set an action command to each button to identify them.

Upvotes: 1

Related Questions