Link to home
Start Free TrialLog in
Avatar of Omer-Pitou
Omer-Pitou

asked on

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Omer-Pitou
Omer-Pitou

ASKER

Thanks. I ll give it a try
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

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...
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.
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.
Unless you have a very large number of fields, i don't see how looping/arrays would help
Yes this is mainly for windows with multiple fields
How many fields are we talking about?
About 50 (manufacturing system)
And even more in other applications
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?
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...
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
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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I try that one and let you know if I have a problem.