mike157
mike157

Reputation: 63

How to check if there is text within my JButton

I want to check whether there is something in my JButton. What would i insert into the equalsIgnoreCase() area?

if (jButton1.getText().equalsIgnoreCase("") &&
    jButton2.getText().equalsIgnoreCase("") &&
    jButton3.getText().equalsIgnoreCase("") &&
    jButton4.getText().equalsIgnoreCase("") &&
    jButton5.getText().equalsIgnoreCase("") &&
    jButton6.getText().equalsIgnoreCase("") &&
    jButton7.getText().equalsIgnoreCase("") &&
    jButton8.getText().equalsIgnoreCase("") &&
    jButton9.getText().equalsIgnoreCase(""))

Upvotes: 1

Views: 1818

Answers (4)

inanutshellus
inanutshellus

Reputation: 10001

You can use

jButton1.getText().isEmpty() 

(use ! to negate if you want to know it's not empty...)

or you can check the length of the value

jButton1.getText().length > 0

Upvotes: 1

shift66
shift66

Reputation: 11958

Nothing, just what you did. But you should put exclamation symbols before conditions to negate them:

if (!jButton1.getText().equals("") &&
    !jButton2.getText().equals("") &&
    !jButton3.getText().equals("") &&
    !jButton4.getText().equals("") &&
    !jButton5.getText().equals("") &&
    !jButton6.getText().equals("") &&
    !jButton7.getText().equals("") &&
    !jButton8.getText().equals("") &&
    !jButton9.getText().equals(""))

In this case body of if will be executed if all your JButtons have some text. And ignoreCase is not necessary. Emptiness has no lower or upper case ))

Upvotes: 0

Frederik.L
Frederik.L

Reputation: 5620

you can use

bool somethingIn = !jButton1.getText().isEmpty();

Upvotes: 0

assylias
assylias

Reputation: 328669

To check that there is a text, you can do:

!jButton1.getText().isEmpty()

or, if you want to exclude a text that only contains spaces:

!jButton1.getText().trim().isEmpty()

Upvotes: 1

Related Questions