aspose file tools*
The moose likes Beginning Java and the fly likes Help with searching through jtextboxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with searching through jtextboxes" Watch "Help with searching through jtextboxes" New topic
Author

Help with searching through jtextboxes

Jake Wilson
Greenhorn

Joined: Jan 26, 2009
Posts: 3
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

> 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?


Steve
Jake Wilson
Greenhorn

Joined: Jan 26, 2009
Posts: 3
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
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
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

Joined: Jan 26, 2009
Posts: 3
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

Joined: Oct 13, 2005
Posts: 40027
    
  28
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with searching through jtextboxes