Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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: 5126
11
Java Netbeans IDE Opera
  • 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
    Pie
    Posts: 20532
    54
    Chrome Eclipse IDE Java 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
    Pie
    Posts: 20532
    54
    Chrome Eclipse IDE Java 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: 5126
    11
    Java Netbeans IDE Opera
    • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic