Adenloolfly
Adenloolfly

Reputation: 3

Is it possible to check for text in a GUI button?

This is a really simple code where when I click a button, the text on the button becomes "..." which is a string variable called move.

What I'm trying to figure out is if it is possible to check the text of a button in the if (e.getSource()==) command.

JButton [] button;
String move="...";

button =new JButton[25];
for (int a=0;a<25;a++)
{
    button[a]=new JButton();
    p1.add(button[a]); 
    button[a].addActionListener(this);
}   

public void actionPerformed(ActionEvent e)
{
    for (int a=0; a<25;a++)
    {
        if (e.getSource()==button[a])
        {
          button[a].setText(move);
        }
    }
}

So after the code above, a button will have the text "..." on it. Now what I want for the next if (e.getSource()==) command is: if the button I click has the text (move), run the code.

I tried hundreds of random codes to see if they'll work but no luck:

sort of like this:

if (e.getSource()==button[a].text(move))

OR

if (e.getSource()==button[a].getText.equals(move))

if (The button i click has the text (move) it will run this code)
{
    button[a].setBackground(Color.GREEN); 
}

I'm fairly new to programming and I need this for my Checkers game. Is it possible to do what I am asking for?

Upvotes: 0

Views: 101

Answers (3)

ChiefTwoPencils
ChiefTwoPencils

Reputation: 13930

The examples you have checking the source against the button text doesn't make a lot of sense. You're adding an action listener on the button not a field of the button. Additionally, getSource returns an Object so if you need to get to any of its member values you'll need to cast it to the type it should be. In your case you're returned an Object with an actual type JButton.

JButton button = new JButton("Hello");
button.addActionListener((e) -> System.out.println(
        e.getSource() instanceof JButton
));
button.doClick();

true

So, given a button you can get the text like so:

JButton button = new JButton("Hello");
button.addActionListener((e) -> System.out.println(
    ((JButton)e.getSource()).getText()
));
button.doClick();

Hello

So, to check move against it would be like so:

String move = "Howdy";
JButton button = new JButton("Hello");
button.addActionListener((e) -> System.out.println(
    ((JButton) e.getSource()).getText().equals(move)
));
button.doClick();

false

Another thing to note is when you get an instance of a button through getSource you don't need to find that instance in the array before using it; it's returned to you via the action listener. For example, run this code:

JButton[] buttons = new JButton[10];
Random r = new Random(System.currentTimeMillis());
String newText = "New";
for (int i = 0; i < buttons.length; ++i) {
    buttons[i] = new JButton(String.valueOf(r.nextInt()));
    buttons[i].addActionListener((e) -> ((JButton)e.getSource()).setText(newText + " " + r.nextInt()));
}
Arrays.stream(buttons).forEach(b -> {
    System.out.println(b.getText());
    b.doClick();
    System.out.println(b.getText());
});

Upvotes: 0

Irfin
Irfin

Reputation: 1

Maybe you mean like this:

if (e.getSource() == button[a]) {
    if (button[a].getText().equals(move)) {
        // do some logic here
        button[a].setBackground(Color.GREEN);
    }
}

Upvotes: 0

Andrew_CS
Andrew_CS

Reputation: 2560

String comparison in Java should be done with .equals() not ==.

Upvotes: 1

Related Questions