0

I have Vector named "datas". Vector looks like;

[1000, 3, 676767, true]

And i have jTextField named "p3_1000"

I write below code for set this text field as invisible or visible.

for(int i = 0; i<= datas.size(); i++){

     if(datas.elementAt(i).elementAt(3).equals("true")){

      ???

     }

System.out.println("p"+(datas.elementAt(i).elementAt(1))+"_"+(datas.elementAt(i).elementAt(0)));
}

Print Line show correct format "p3_1000" as like my textfield name. But i dont find a ways set releated textfield call with its name. How i write this code for automaticly detect true textfield ?

I must say if 3th index of current vector row is "true" set visible textfield named "p"+(datas.elementAt(i).elementAt(1))+"_"+(datas.elementAt(i).elementAt(0))

But how?

Can we call any object with its name using any string referance ?

Black White
  • 700
  • 3
  • 11
  • 31
  • you can put a name to your textfield with this method `setName` – nachokk Mar 06 '14 at 17:06
  • @nachokk i have already named 100 textfield. 1 and 0 index element of Each vector rows equal my textfields as you can see. I just call releated text field when i loop vector rows. – Black White Mar 06 '14 at 17:12

1 Answers1

2

datas.elementAt(i).elementAt(3)=="true" doesn't work as you expect in Java. == is used to compare the equivalence of two objects, and what you have here are two different objects.

You're looking for datas.elementAt(i).elementAt(3).equals("true") which compares the contents of the strings.

If you're looking for the code to make a text field visible or invisible, use setVisible(true) or setVisible(false) - see also this post: setVisible(false) to a group of JTextField and JLabel

Finally, for the part where you're trying to use string composition to determine the name of an instance of a JTextField - I would suggest that there is a better approach. Maintain a HashMap where the string that you compose is the key to the map, and you can always get the corresponding text field by doing map.get(key) instead of trying to create the name of the variable... which would require reflection, and that's not a path you need to go down.

Community
  • 1
  • 1
David Koelle
  • 20,726
  • 23
  • 93
  • 130
  • It's not clear what you're looking for in the ??? line - are you asking for the code that makes the text field visible? – David Koelle Mar 06 '14 at 16:59
  • Yes i want make named "p3_1000" text field as visible. But i dont use p3_1000.set... command. Because i have many textfield and true textfield detect from datas vector like in print line control. – Black White Mar 06 '14 at 17:02
  • p3_1000.setVisible(true); working i know. But releated textfield name must be detect from Vector releated values. like "p"+(datas.elementAt(i).elementAt(1))+"_"+(datas.elementAt(i).elementAt(0)) – Black White Mar 06 '14 at 17:04
  • I cant use group. Because i have 100 textfield and each textfield must be controlled one by one. – Black White Mar 06 '14 at 17:15
  • Thanks for the HashMap advice. I dont use it before and concentrate on it with your advice. I find [THIS LINK](http://stackoverflow.com/questions/4958600/get-a-swing-component-by-name) about call components by their names. It solve my problem completely. Thanks for all. – Black White Mar 07 '14 at 16:11