Avatar of ChartTrack
ChartTrack

asked on 

Creating comboboxes dynamically

I have a panel which i need to add combo boxes as needed.
e.g. start with a blank panel and through an increment and decrement button on a seperate panel, create/remove new comboboxes on the blank panel.
Java

Avatar of undefined
Last Comment
Mick Barry
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Use a vertical BoxLayout and just add/remove them as needed. Call validate and repaint after doing so
Avatar of ChartTrack
ChartTrack

ASKER

That's my problem. I don't know how to dynamically create them.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

try something like this:

JComboBox combo =new JComboBox();
// initilise combo
panel.add(combo);
panel.revalidate();
panel.repaint();

you'd call it from your buttons action listener

Avatar of ChartTrack
ChartTrack

ASKER

the name of all the comboboxes will be 'combo'. How do I differentiate between them?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need something like

void addCombo(ComboBoxModel model) {      
      JComboBox cb = new JComboBox(model);
      combosList.add(cb);// Keep in collection so the reference not lost
      panel.add(cb);
      validate();
      repaint();
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>How do I differentiate between them?

Rather than use a List as i suggested, you could also use a Map
Avatar of Mick Barry
Mick Barry
Flag of Australia image

>       validate();

should be revalidate()

see the code i posted above

Avatar of Mick Barry
Mick Barry
Flag of Australia image

> You need something like

and thats already been mention, please have read of all other comments before posting to avoid confusing the asker

Avatar of ChartTrack
ChartTrack

ASKER

>> combosList.add(cb)

what is actually being stored here? And how would I use it to gain access to the combobox.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> what is actually being stored here? And how would I use it to gain access to the combobox.

the combo, but its unnecessary and makes it harder to maintain.
You should avoid references to every component in your gui to make your app easier to manage.
Instead use listeners that react when a change occurs in the gui.

Avatar of ChartTrack
ChartTrack

ASKER

but I still need to attach data to the combobox (datamodel?) in the first instance then retrieve at some point.
My question is (and this is where I show myself up as a begineer) how do I know which combo box it is as they are all havethe same name. Or am i missing a trick here ;)
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>what is actually being stored here? And how would I use it to gain access to the combobox.

The reference to the combo so that it can be accessed. Listeners, btw, are there to provide messaging capabilities between components, *not* to provide references, which you will need to use for direct access. Furthermore, if you don't have a reference to the component in the GUI, it will actually disappear
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>how do I know which combo box it is as they are all havethe same name. Or am i missing a trick here ;)

A name and a reference are different things. How do you *want* to differentiate between them? IOW how do you know by looking which is which?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

typically you create your model when you create the combo, even if its empty.
You can then get the state from the model. You rarely need to access the combo directly once it has been added to your combo.
Plus references to the combo (and model) are passed to the listner when a change occurs.
(and the panel also stores references to the combos if you're desperate)


Avatar of ChartTrack
ChartTrack

ASKER

Your underestimating my ignorance.
Say, I have pressed the 'add' button three times and created 3 comboboxes. For argumants sake they all contain lists of countries. My user has selected three different countries (one for each combo) and I now need to do something with it.
Because I know how many there are I could loop through the List I created, has you suggested CEHJ, is it a case of

Class comboHolder;
comboHolder = comboList(i);

Does the Class (comboHolder) I created now behave as the class JCombobox??

Please excuse the poor terminlogy!!
Avatar of ChartTrack
ChartTrack

ASKER

objects - I wrotemy reply before refreshing and didn't see you comment.
If I use a listener - then again I need to know which combo i am listening to - that's the bit I don't understand. All I know is that is belongs to a certain panel. I have no means of establishing which it may be.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo