• Post Reply Bookmark Topic Watch Topic
  • New Topic

check .isEnabled of a JTextField in an ArrayList of JTextFields and get text.  RSS feed

 
Dan Nicholson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList of JTextFields in a CAMSetup1 class that contains a bunch of named JTextFields: Layer_textField_1, Layer_textField_2, Layer_textField_3, ... etc

I would like to check the .isEnabled status of a particular JTextField in that ArrayList from my main class by passing the name of the JTextField to a method in the CAMSetup1 class as a string

The peice of code calling the method from the main class is something like this:


The method for checking the .isEnabled that I have so far is:


I also need help with getting the text:


Can someone help me out please?
Thanks!



 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds awkward. Why don't you put the fields into a Map<String, JTextField> and you can retrieve them easily from the text.

And welcome to the Ranch
 
Dan Nicholson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
I don't have any experience at all using a Map. I'll have to read up on that option. While using Maps could I call a jtextfield object by using a string of the jtextfield name?
Thanks!
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Nicholson wrote: . . .
String filename = false;
. . .
That is a new one to me

Don't use == true or == false, which are both poor style and error‑prone if you mistakenly write =
Not if (b == true) … nor
if (b == false) …
but
if (b) … and
if (!b) …

please.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Nicholson wrote:Hi Campbell,
I don't have any experience at all using a Map. I'll have to read up on that option. While using Maps could I call a jtextfield object by using a string of the jtextfield name?
Thanks!
Start here. You will find two sections about Maps under interfaces and implementations.

You would have to create some sort of String as a “K” and use the text field object as a “V”.

If you already have them ordered in a List, you can iterate the List and put them into a Map like this:-
textMap.put("textField" + i, myList.get(i));

Get them back like this:-
JTextField field = textMap.get("textField99");

Beware: A HashMap will happily return null if you give it a non‑existent “K” in the get method.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find nice ways to create a Map from the List with a Stream in the tutorials link I gave you.
 
Dan Nicholson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much,
I'll do some studying on that link you gave me and try to update the method I'm using.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!