Jackie
Jackie

Reputation: 15

Having issues changing button text in Java Swing

I'm trying to test ways to change a button's text via an actionlistener. Here's my code:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TCTBack implements ActionListener {

    JFrame frame;
    JPanel panel;
    JLabel label1;
    JButton button1;
    String turn;

    public TCTBack() {

        turn = "-";

        panel = new JPanel();
        panel.setSize(500, 500);

        frame = new JFrame();
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);

        panel.setLayout(null);

        label1 = new JLabel("Interactive Button Test");
        label1.setBounds(10,20,80,25);
        panel.add(label1);

        button1 = new JButton("Button");
        button1.setBounds(10,80,80,25);
        button1.addActionListener(this);
        panel.add(button1);

        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        switch (turn) {
            case "-" -> {
                turn = "X";
                button1.setText("X");
            }
            case "x" -> {
                turn = "O";
                button1.setText("O");
            }
            case "O" -> {
                turn = "-";
                button1.setText("-");
            }
        }

    }

    public static void main(String[] args) {

        new TCTBack();

    }

}

It changes the button's text once, but won't change it after I click the button again. My goal is to be able to change the button's text from the default value("-") to X, then to O, then back to -. Any help would be appreciated, thanks. Also, I'm a newbie at asking questions so if I'm missing any information then just let me know.

Upvotes: 0

Views: 294

Answers (3)

Abra
Abra

Reputation: 20914

Define constants. Using constants prevents errors like yours.
Here are the parts of your code that I suggest you change with proposed changes. Explanations after the code.

public class TCTBack implements ActionListener {
    private static final String NONE = "-";
    private static final String O = "O";
    private static final String X = "X";

    public TCTBack() {
        button1 = new JButton(NONE);
    }

    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        String newVal;
        switch (actionCommand) {
            case NONE:
                newVal = X;
                break;
            case O:
                newVal = NONE;
                break;
            case X:
                newVal = O;
                break;
            default:
                newVal = null;
                JOPtionPane.showMessageDialog(frame,
                                              actionCommand,
                                              "Unhandled",
                                              JOptionPane.WARNING_MESSAGE);
        }
        if (newVal != null) {
            button1.setActionCommand(newVal);
            button1.setText(newVal);
        }
    }
}

When you create a JButton using the constructor that accepts a single, String argument, you set both the button's text and its action command. The ActionEvent object passed to method actionPerformed() contins this action command string.

However, calling method setText() of class JButton does not change the action command, hence you need to also update that via method setActionCommand().

Upvotes: 0

rzwitserloot
rzwitserloot

Reputation: 102820

turn = "X"

case "x"

One is a capital X, and one is not.

Tip: If you think a switch is 'exhaustive' (a case statement should always be matched), add a default with throw new IllegalStateException(); or some such. Or use the switch as an expression, in which case java will give you that for free.

Upvotes: 0

stackstack293
stackstack293

Reputation: 349

Because you change the text to X but you check if it equals x

case "X" -> {
    ...
}

Upvotes: 1

Related Questions