Add a component on the fly
Hi!
I am writing a program where the expected number of components is unknown. It depends on various factors and I have no control over it. My problem is this;
I can create a component (say JTextField())and add it to my JPanel. But how can I access it again to either read or modify the values? If it was just one component it would not be a problem. But for 'n' number of components what is the solution?
Any help will be appreciated.
Keep them in an array or ArrayList and access them through that.
Then again, if you have a GUI that can "look like anything", you're gonna have a hard time coding business logic for it...
Kayaman & CeciNEstPasUnProgrammeur,
Thanks for your input.
I cannot use an array as I do not know the number of components in advance. I could use JTable but I am not doing so now.
I have decided to use Vector as components can be added dynamically and retrieved. I have made sure that I have individual Vector classes for different group of items - one for JComboboxes and another for JTextFields. The challenge was how to change the textfield's values based on the combobox's selection. I overcame that by using setActionCommand() method to the combobox and listening for events. Do you think this is a good way or does it consume a lot of memory? Any input will be appreciated.