• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with searching through jtextboxes

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, im using netbeans to do some GUI making, and need some help. I have a large grid of textboxes (6 x 8), and I need some way to search through them. I tried to add them to an arraylist, but its not working for some reason.

ArrayList<JtextField> texts = new ArrayList<JTextField>();

texts.add(A1Box); //A1Box is a previously declared JTextBox

It says it cant find A1Box, even though i know it has been declared / initialized already.

so is there some other way I can search through all the boxes on the page? I need to do a lot of code to each box, so just searching through them all manually with a case statement would definitly not work if i need to change something.

Thanks!!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> It says it cant find A1Box, even though i know it has been declared / initialized already.

That suggests that A1Box was declared in a different scope than the one you are trying to use to store it in the ArrayList.

What exactly are you trying to do? Why do you need to look up the text boxes?
 
Jake Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I thought the scope thing as well, but im pretty sure that its all in the same scope. I need to search through all the textboxes, get the name out of them and do a couple things to them. Nothing that requires a ton of code. So was the way that I did it the right way?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Wilson wrote:Yeah, I thought the scope thing as well, but im pretty sure that its all in the same scope. I need to search through all the textboxes, get the name out of them and do a couple things to them. Nothing that requires a ton of code. So was the way that I did it the right way?



Well, it depends on the error -- is this a compile time error? or a runtime error? At compile time, if it can't find the reference, it is generally because you are using it out of scope... can you show us some code? .... And what do you mean by "pretty sure" -- either it is, or it isn't.

At runtime, how are you searching for it? The JTextField doesn't override the equals method, so the only way to find the JTextField in question, is if you already have a reference to it. It doesn't make sense to "search" for it.

Henry
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch
You could put your JTextFields into an array with two sets of square brackets (JTextField[][]). That might make it easier to handle when you set up your layout. And you can iterate through the array with [two nested] for loops or for-each loops.
 
Jake Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to JavaRanch
You could put your JTextFields into an array with two sets of square brackets (JTextField[][]). That might make it easier to handle when you set up your layout. And you can iterate through the array with [two nested] for loops or for-each loops.



Except i cant put them in anything, for some reason the scope is off. I dont have to do anything special to put the textboxes into the array do it?

arraylist.add(textbox) should work right?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your JTextBox[][] or your List<JTextBox> is a field, then even though the individual boxes go out of scope, the list or array will remain in scope for ever.
You can then retrieve the individual boxes from the array or from the List.
And I think I said JTextBox when I meant JTextField, but I am sure you know what I mean
reply
    Bookmark Topic Watch Topic
  • New Topic