This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Onchange JCombobox

 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellow Java lovers,

I have two swing combo boxes. We will call the first one parent, and the second one dependant. When the dialog box is drawn, a search in a database is performed to get the correct Object[] (depends on the user and what the user has saved to date), for the parent JCombobox. What I want is, when the user changes their selection in the parent JCombobox, that the program searches the database and delivers the correct Object[] for the dependent. Short of making the user decide which parent choice he wants before the primary dialog is opened, how do I go about writing the code to update the dependent JCombobox when the parent is changed?

Thank you!!

 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's holding you up?
  • detecting a selection change in the first combo?
  • changing the content of the second combo?
  •  
    Daniel Marti
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Write a listener for the JComboBox, listen for the actionPerformed event, get the choice in the parent JComboBox (parent.getSelectedItem()) and then populate the dependant JComboBox the way you want.
     
    Rob Spoor
    Sheriff
    Posts: 21774
    103
    Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You start with adding an ActionListener to the parent combo box. This will be triggered when a new value is selected. If this occurs, you need to find the Object[] for the parent combo box' current selected (getSelectedItem()). A Map<Object,Object[]> is the easiest way to perform this lookup. Next you either update the existing model for the dependent combo box, or create a new ComboBoxModel for the new content and set that model.
     
    Greg Reeder
    Ranch Hand
    Posts: 99
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My main point is that I dont know where to begin, so to answer your questions, I am a bit stumped with both aspects.
     
    Greg Reeder
    Ranch Hand
    Posts: 99
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How do I update the current model?
     
    Daniel Marti
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Greg Reeder wrote:How do I update the current model?



    If you mean update the dependat JComboBox with new values, simply clean it (if it has previous values) with dependant.removeAllItems, and then add the new items by using dependant.addItem .
     
    Greg Reeder
    Ranch Hand
    Posts: 99
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    After trying that, I got this error:

    Exception occurred during event dispatching:
    java.lang.NullPointerException



    Is it wrong to try to add an entire object[] as the object?
     
    Greg Reeder
    Ranch Hand
    Posts: 99
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That error message was just my stupidity. I forget to say that a is a certain object and not null. I now have an object in the JCombobox, but the object is the array, and not the contents of the array. Is it possible to add an array after the JCombobox is made?
     
    Rob Spoor
    Sheriff
    Posts: 21774
    103
    Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You now call vaccCB.addItem(o). Why don't you call vaccCB.addItem for each separate element of the array instead?
     
    Greg Reeder
    Ranch Hand
    Posts: 99
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I ended up using a foreach loop to add each object in the object array seperately. I find it dumb that you cannot just add the whole array, or at least if you can, i didnt figure it out, but this works. Thank you all for your help.
     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Netbeans IDE Opera Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can construct a DefaultComboBoxModel with the array, and setModel(...) on the combo box.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!