How to create a collection and add text fields

  • Thread starter chief african brewer
  • Start date
C

chief african brewer

Hi all,
I have this problem. I want to be able to create a collection that I
can aadd some or all of my text fields in a panel so that I can
traverse them. Th reason I want to do that is because I need to do
some tests on my text fiels,e.g count the number of text fields whic
are blank,i.e have "" .
Thanks in advance.
 
R

Roedy Green

I have this problem. I want to be able to create a collection that I
can aadd some or all of my text fields in a panel so that I can
traverse them. Th reason I want to do that is because I need to do
some tests on my text fiels,e.g count the number of text fields whic
are blank,i.e have "" .
Thanks in advance.

Any Collection will work just fine. Most of the time a simple array
will suffice. You just allocate your GUI objects with new, then add
them to a collection so you can find them again.

Most of what you do is event handlers which just ignore your
collection.

see http://mindprod.com/jgloss/collection.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
~ Richard Heinberg, The Party’s Over: Oil, War, and the Fate of Industrial Societies
 
M

markspace

chief said:
Hi all,
I have this problem. I want to be able to create a collection that I
can aadd some or all of my text fields in a panel so that I can
traverse them. Th reason I want to do that is because I need to do
some tests on my text fiels,e.g count the number of text fields whic
are blank,i.e have "" .
Thanks in advance.


Sabine and Roedy almost got it, but they missed one important point:
Swing text fields are not thread safe, and must be synchronized.

Building on the advice above, use whatever collection you think best,
then synchronize it:

List<JComponent> fields = Collections.synchronizedList(
new ArrayList<JComponent>() );

There are other synchronization methods in the Collections class if you
choose a different type of collection:

<http://java.sun.com/javase/6/docs/a...#synchronizedCollection(java.util.Collection)>

There's six total there so make sure to scroll down and read them all.

To iterate over this collection, you MUST synchronize the whole
collection before you get the iterator, and hold the synchronization
lock while you use the iterator:

synchronized( fields ) {
Iterator<JComponent> i = fields.iterator();
while( i.hasNext() ) {
...
}
}

This hold true for the "for each" version of this loop, which uses an
Iterator internally.

Anything else will just blow up intermittently and be really hard to debug.
 
R

Roedy Green

Sabine and Roedy almost got it, but they missed one important point:
Swing text fields are not thread safe, and must be synchronized.

Building on the advice above, use whatever collection you think best,
then synchronize it:

List<JComponent> fields = Collections.synchronizedList(
new ArrayList<JComponent>() );

It is a little worse than that. You must do your fiddling on the EDT
thread. SynchronisedList won't keep Swing from executing methods on
your GUI objects willy nilly.

See http://mindprod.com/jgloss/swingthreads.html

Threads are always tricky, but you likely want do do everything to
your GUI objects with invokeLater and brethren.

--
Roedy Green Canadian Mind Products
http://mindprod.com

"The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
~ Richard Heinberg, The Party’s Over: Oil, War, and the Fate of Industrial Societies
 
M

markspace

Roedy said:
It is a little worse than that. You must do your fiddling on the EDT
thread. SynchronisedList won't keep Swing from executing methods on
your GUI objects willy nilly.


"setText(String)" is thread safe, he could call that, but he still needs
a valid reference (which a synchronized collection will provide).

I think every other method in JTextField is not thread safe (including,
oddly, "getText()") so as a practical matter you're right, mostly likely
he'll have to synchronize by executing on the EDT.
 
A

african brewer

Thanks all for the answers. However, i still cannot figure out how to
only add the components in one panel into the collection.
This is because i have many text fields in a panel that i want to add
to the collection. The panel has so many other components like labels.
I cannot add them manually,i want to just access them e.g
pnlMiddle.getcomponents()
Please help.
 
J

John B. Matthews

african brewer said:
Thanks all for the answers. However, i still cannot figure out how to
only add the components in one panel into the collection. This is
because i have many text fields in a panel that i want to add to the
collection. The panel has so many other components like labels. I
cannot add them manually,i want to just access them e.g
pnlMiddle.getcomponents()

Given a list such as this:

List<JTextField> list = new ArrayList<JTextField>();

Then add each field to the list as you add it to the panel:
....
JTextField tf1 = new JTextField();
panel.add(tf1);
list.add(tf1);
JTextField tf2 = new JTextField();
panel.add(tf2);
list.add(tf2);
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top