Get all the components on a container (JFrame, JDialog)

Hi Sirs,
This is what I am trying to achieve (I did that in dBASE).
1. I have a container with multiple objects on it (textField, textArea, Spinner, CheckBox, Combobox)
2. Those objects meet a specific naming convention (EF_employeID, EF_lastName, etc..). EF meaning EntryField on a Container, after employeID is the related table column name, and only components starting will go to my database. I can have different prefix.
3. When a JDialog is called, I want to have all its components in an Array
4. From that array, I want to be able to generate my INSERT/UPDATE SQL Statement String
5. From  that array, I want to be able to loop it in order to clear fields on the container or display content of a resultSet (Edit).

This process saves me a lot of time when developing and maintaining.

Did someone achieve this?
Thank you in advance for your time.

Regards.
Omer-PitouAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You could use an array but also, using java.awt.Container.getComponents and java.awt.Component.setName, java.awt.Component.getName  will do it for you. You should use Swing components
0
Omer-PitouAuthor Commented:
Thanks. I ll give it a try
0
CEHJCommented:
On balance, an array is a much better option. e.g.


JTextComponent[] persistenceFields = new JTextComponent[NUM_PERSISTENCE_FIELDS];
...
persistenceFields[0] = textFieldOne;
persistenceFields[1] = textFieldTwo;
persistenceFields[2] = textAreaOne;

Open in new window

0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Omer-PitouAuthor Commented:
Thank you so much. That is almost what I am looking for.
Is there a way to get all the components with a single statement and load them in a Object Array. In the example above you re adding one by one
I tried something like this without success. It gives me incompatible types error and forces me to awt.Component instead
JComponent[] comp = panel.getComponents();
         for (JComponent item : comp) {
            System.out.println(item.getName());
        }
I am interested to load only these components: TextField, Combobox, CheckBox, TextArea, Spinner whose name starts with "EF". Or should I just do the filter in a loop combined with a if statement...
0
CEHJCommented:
I am interested to load only these components: TextField, Combobox, CheckBox, TextArea, Spinner whose name starts with "EF".
How is that going to help you? All those components have quite heterogeneous ways of getting their values. I was originally imagining all subclasses of JTextComponent (hence the array type) but your use case won't fit that.
0
Omer-PitouAuthor Commented:
Hi,
I know some use get/set Text and other use get/set Value... This I can handle in a loop.
Let's put it this way, besides JTextComponent, Component classes, which are other classes do the same job, I mean the role to hold others objects no matter their respective class.

Regards.
0
CEHJCommented:
Unless you have a very large number of fields, i don't see how looping/arrays would help
0
Omer-PitouAuthor Commented:
Yes this is mainly for windows with multiple fields
0
CEHJCommented:
How many fields are we talking about?
0
Omer-PitouAuthor Commented:
About 50 (manufacturing system)
0
Omer-PitouAuthor Commented:
And even more in other applications
0
CEHJCommented:
OK, that might make quite a difference. Let's try to do the best we can. Can you tell me what you'll do when you've got the value - are you going to do a db insert or what?
0
Omer-PitouAuthor Commented:
Sorry to reply late.
With the values gotten  for each component whose name starts with EF, I will build a SQL string like
INSERT INTO table(field1, field2, field3,...) VALUES (valuefield1, valuefield2, value3,....).
This string is built while looping through the array holding the components whose name starts with EF.
i.e Component name EF_EMPLOYEID: The field1 will be EMPLOYEID and the value1 will ef_employeID.getText() or ef_employeID.getValue() depending on type on component.

That string will be sent straight to the execute method of the Statement interface.

Hope  this clarify it.

And the same Array will be used for edit a record, to empty the form, etc...
0
CEHJCommented:
OK but your loop is going to have to check the name and then decide whether it's going to be getText or getValue or something like that
0
Omer-PitouAuthor Commented:
That is right. In my dBASE program, I was using a prefix in the name itselft (3rd position) EFC_EMPLOYE -> Character ... This can be handled in different ways: in the name structure itself  or with getClass() method (I think).
My  only challenge is to load them in an Array.
0
CEHJCommented:
OK.  The closest you're going to get is something like this
int componentNamePlaceholderNumber = Integer.parseInt(c.getName());
String value = null;
Component c = components[i];
if (c instanceof JTextComponent) {
  value = c.getText();
}
else {
 ...
}
preparedStatement.setString(componentNamePlaceholderNumber, value);
....

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Omer-PitouAuthor Commented:
I try that one and let you know if I have a problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.