Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with searching through jtextboxes

 
Jake Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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!!
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic