swing: identifying  dynamically generated controls

guyneo
guyneo used Ask the Experts™
on
Hi experts,
I need your help in figuring this out.The issue is how to change attributes of controls that has been dynamically generated.  The first challenge seems to be able to get the name/id of the component. How do you guys recommend I do this?
example : User can select a number between 1 to 10. If 7 is selected. I want to create seven controls(Textfields). After generating all of them, I have to set the size and other attributes based on some rules. How can I  get the fifth text box and set it the color to red? setname is one attribute I thought of using to solve this. Whats the best way to do this?
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Just use an array and address by index


JTextField[] fields = new JTextField[];
for(int i = 0;i<fields.length;i++) {
    fields[i] = new JTextField();
}
fields[4].setText("I'm number five!");

Open in new window

Top Expert 2016

Commented:
>>JTextField[] fields = new JTextField[];

was meant to be

JTextField[] fields = new JTextField[7];
Mick BarryJava Developer
Top Expert 2010

Commented:
Using name is the usual method used, you can then use a Map to make it easy to lookup any component you need.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
objects.... how can you access the name  since the name is also dynamically generated with the component? I have done this before in javascript because it has evaluate command.

CEHJ thanks for that suggestion. I am sure it will work. For some reason I drew blank and thought that once you add to an array it can't be added to the Jpanel and made visible. Sounds stupid I know. I will try it out and let you guys know.
Top Expert 2016

Commented:
>>it can't be added to the Jpanel and made visible.

Yes, it's fine, it can. If you want to set the name of the component, though what you'd need it for i'm not sure, you'd do:
fields[i].setName(String.format("Field%d", i));

Open in new window

Author

Commented:
sorry Objects.. I kind of missed what I wanted to say. I meant to say that I use this approach when I was using  object oriented javascript. However I didnot use maps in swing. So I didnot understand your solution very well. Did you mean a action map? or something else.  I will need more direction and example. I can learn something new.
Mick BarryJava Developer
Top Expert 2010
Commented:
>  since the name is also dynamically generated with the component?

you can set the name to whatever you want

component.setName("whatever");

You could then use a Map to store your components

Map lookup = new HashMap();
lookup.put(component.getName(), component);


Then when you need a component you can look it up by name

JComponent component = lookup.get("whatever");

So you can now easily access your components by name, nice and easy :)


Author

Commented:
Thanks objects. I learnt a new way of doing this. I think it will be pretty useful. CEHJ Thanks your  response was timely and perfect.
Top Expert 2016

Commented:
:-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial