Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic JComboBoxes

Status
Not open for further replies.

rocknrisk

Programmer
May 14, 2002
43
GB
Hi all,

Please can someone help me. I need to create a series of JComboBoxes that are dynamic (the list in JComboBox2 must change depending on the sellection in JComboBox1).

So, basically, if the value in JComboBox1 is "A", JComboBox2 lists "Apple", "Orange" and "Pear". But if the selection in JComboBox1 is "B", JComboBox2 lists "Guitar", "Bass" and "Keyboard".

I hope I'm making sense here. [dazed] I think I've got the if statements figured out but what methods in the JComboBox class do I use?

Thank you in advance for any help here.
Clinton [smile] "The important thing is not to stop questioning." - Albert Einstein
 
Ok, first off, I haven't done what you are trying to achieve (although I have achieved similar things using Jlists). Now with that out of the way, here's my advice :)

A really good place to start is the SwingSet2 demo that comes with the J2SE. It shows a lot of neat Swing tricks AND all the source code required to make them work, it is here that I figured out how to do the dynamic JLists.

This is copied straight out of the Java documentation for JList
--------
The contents of a JList can be dynamic, in other words, the list elements can change value and the size of the list can change
after the JList has been created. The JList observes changes in its model with a swing.event.ListDataListener
implementation. A correct implementation of ListModel notifies it's listeners each time a change occurs. The changes are
characterized by a swing.event.ListDataEvent, which identifies the range of list indices that have been modified,
added, or removed. Simple dynamic-content JList applications can use the DefaultListModel class to store list
elements. This class implements the ListModel interface and provides the java.util.Vector API as well.
Applications that need to provide custom ListModel implementations can subclass AbstractListModel, which
provides basic ListDataListener support.
--------


Read the documentation that comes with Java, it is very rarely lacking. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top