Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Using a JComboBox to change the contents of another JComboBox

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm new by the way.

I would like to make a JComboBox change the contents of another JComboBox when one of it's values has been selected with the mouse.

Not really sure how to go about this (I'm the grandfather of the original newb)

So far, my code is this;



I'm thinking of using the contentChanged() method in the actionPerformed event but not quite sure how I would go about it.

Also, how do listdatatypes/listdatalisteners work?

Thanks
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class implements ActionListener, but you have not registerd your comboboxes to listen for actions. Thats the first thing to do:


And then, in the actionPerformed method:


ListSelectionListener is not for combo boxes. They are used for JList and JTable:
http://download.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply

I just need to know the method to call to change the contents of the JComboBox, any idea how to go about doing that?

 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All data displayed in the JComboBox comes from the underlying model. To change the contents just (re)set the model.(hint)
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm...nope, I can't figure it out, I tried this but it didn't work;



Not sure if thats right even in the slightest, but I'm really new to java, any advice? I'm sorry for pretty much asking you to do it for me, just I really am clueless.

Thanks again
 
Maneesh Godbole
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are just recreating it.
Check the API docs for setModel().
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the API for setModel() , found it, but I don't understand what a ComboBoxModel is, any ideas?
 
Maneesh Godbole
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API docs, emphasis mine.

A data model for a combo box. This interface extends ListDataModel and adds the concept of a selected item. The selected item is generally the item which is visible in the combo box display area.


As you can see this is an interface. If you browse further through the API docs, you will notice DefaultComboBoxModel offers the concrete default implementation of this interface.
Check out the API to see how you can use this model to wrap your data.
Essential you need to create an instance of this model and then pass it to the setModel()
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate to say it but I still don't understand this, I've looked at the API but haven't had much luck.

My miserable piece of code is as follows;




First thing, what will the action listener pick up on? When the combo box is clicked? When one of the values is selected? How do you know?

Can you explain the theory behind how the DefaultComboBoxModel implementation works?

Sorry for not being helpful

Thanks


 
Maneesh Godbole
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the API, you can see that the constructor accepts Object []items
So you can use, in your action performed
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that was a lot easier then I first thought.

And it works great, thank you again for being patient with me.

I'm managing to use other methods to do some more stuff with the combo boxs, seems to going okay from here

I'll let you know if I ever get stuck again.

Thanks
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic