-1

I'm trying to do a loop thru all the button text in a form, but i can't find any solution for Java !!

i did something like :

 for(JButton temp : this)
 {
        if(JButton.getText() == "A")
        {
          JButton.enabled(false);
        }
 }

but no luck

p.s. my problem is not about the equals statement !! I want to loop thru all button in my window.

here is the working result thanks to MadProgrammer:

for(Component comp : jPanel2.getComponents()) 
            {
                if(comp instanceof JButton)
                {
                   JButton btn = (JButton)comp;                       
                   if(btn.getText().equals("A")) 
                   {
                     btn.setEnabled(false); 
                   }
                }
            }
Robyseb
  • 1
  • 4

3 Answers3

1

You have a number of problems...

for(JButton temp : this) simply doesn't make sense, unless this implements Iterable<JButton>

if(JButton.getText() == "A") has two issues. The first is, getText() isn't a static method, so it can't be called in this manner, the second is, == shouldn't be used to compare String values...

JButton.enabled(false); has two issues. The first is, enabled isn't static and enabled is actually depreciated, so you should avoid using it. Instead using setEnabled

Without know how you buttons are actually managed, it's impossible to provide you an accurate solution.

If you are trying to iterate through the buttons on a Container of some kind, you might use something like...

for (Component comp : container.getComponents()) {
    if (comp instanceof JButton) { 
        //...
    }
}

For example.

If the buttons are stored in some kind of java.util.List, you could using something like...

for (JButton button : listOfButtons) {
    //...
}

In order to check the button text, you should use something like (using the previous example as a base)...

if ("A".equals(button.getText()) {...}

Take a look at Creating a GUI with Swing for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hello if i use : for (Component comp : container.getComponents()) { if (comp instanceof JButton) { //... } } how do i get the text of Comp ? – Robyseb Oct 21 '13 at 02:48
  • @Robyseb You cast the reference to a `JButton` first. `JButton btn = (JButton)comp;`, hence the need for `instanceof` – MadProgrammer Oct 21 '13 at 02:49
  • ok great ... i was not spelling it right but now it work perfectly thank you i update my top post with the final result – Robyseb Oct 21 '13 at 02:54
0

Take a look String compare

      if("A".equals(temp.getText())) // or use if("A".equalsIgnoreCase(temp.getText()))
        {
          temp.setEnabled(false);
        }

instead of

      if(JButton.getText() == "A")
        {
          JButton.enabled(false);
        }
Community
  • 1
  • 1
newuser
  • 8,338
  • 2
  • 25
  • 33
  • **FYI**: According to [docs](http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#enable(boolean)), `enable` is *Deprecated*. `setEnabled` should be used instead. – PM 77-1 Oct 21 '13 at 02:06
  • `"A".equals(temp.getText())` will protect you against `NullPointerException` – MadProgrammer Oct 21 '13 at 02:07
0

The problem with your code is == sign. Whenever you compare the values of two objects, here being String, you use the equals() method instead of ==.

The operator == is for primitive types. In case of objects, it will compare the addresses instead of the object's value. On the other hand, equals() will actually compare the values.

Try:

for(JButton temp : listOfButtons) // I changed the iterable here.
 {
        if("A".equals(temp.getText())) // notice who the caller to .equals() is. It is "A"
        {
          temp.setEnabled(false); // make temp disabled
        }
 }  

Not being harsh but the code that you were using was completely flawed. I corrected the errors; see if that works for you.
The errors were: 1. Your source of buttons in the for loop was wrong. 2. getText() was used as a static method while it is dynamic. 3. You used an == instead of equals() 4. setEnabled() was used as a static method instead of dynamic

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • [`getText`](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#getText()) is **not** a `static` method. – PM 77-1 Oct 21 '13 at 01:55
  • @PM77-1 That is OP's code =) I just edited it. – An SO User Oct 21 '13 at 01:56
  • Excuses, excuses, ... – PM 77-1 Oct 21 '13 at 01:57
  • @PM77-1 I actually didn't notice it. I saw the error in the loop and decided to examine further :-P Thanks for linking to the docs of `getText()` though – An SO User Oct 21 '13 at 01:59
  • Hi i understand about the .equals but the problem i have is to iterate thru all button ! i took the example of Little Child but "listOfButtons" cannot be found ?? – Robyseb Oct 21 '13 at 02:02
  • 1
    @Robyseb You stated that you want to loop over the buttons. To loop using the `for:each` loop, you must store them somewhere like in a `Vector<>` or in an `ArrayList<>`. Where did you store the buttons? :) – An SO User Oct 21 '13 at 02:05
  • `"A".equals(temp.getText())` will protect you against `NullPointerException` – MadProgrammer Oct 21 '13 at 02:07
  • Thank you Little child you understand my problem lol ... my button are actually not store anywhere as Netbeans create them as i draw them ! They might be store by default but i dont know where ? – Robyseb Oct 21 '13 at 02:11
  • @Robyseb LoL that is why I prefer to make my GUI via code and not drag and drop ;-) *I prefer Eclipse, btw* – An SO User Oct 21 '13 at 02:12
  • NetBeans create the button with these variable "private javax.swing.JButton jButtonA;" any other idea ? – Robyseb Oct 21 '13 at 02:18
  • So it is creating single instances. You need to learn how to create and store buttons in loops. It is fairly easy =) – An SO User Oct 21 '13 at 02:19