• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Onchange JCombobox

 
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
  •  
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Sheriff
    Posts: 22821
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How do I update the current model?
     
    Daniel Marti
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 22821
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can construct a DefaultComboBoxModel with the array, and setModel(...) on the combo box.
     
    I can't beleive you just said that. Now I need to calm down with this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic