aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Onchange JCombobox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Onchange JCombobox" Watch "Onchange JCombobox" New topic
Author

Onchange JCombobox

Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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

Joined: May 03, 2008
Posts: 4522
    
    5

What's holding you up?
  • detecting a selection change in the first combo?
  • changing the content of the second combo?


  • luck, db
    There are no new questions, but there may be new answers.
    Daniel Marti
    Ranch Hand

    Joined: Jun 08, 2011
    Posts: 37
    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

    Joined: Oct 27, 2005
    Posts: 19538
        
      16

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Greg Reeder
    Ranch Hand

    Joined: Jun 14, 2011
    Posts: 99

    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

    Joined: Jun 14, 2011
    Posts: 99

    How do I update the current model?
    Daniel Marti
    Ranch Hand

    Joined: Jun 08, 2011
    Posts: 37
    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

    Joined: Jun 14, 2011
    Posts: 99

    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

    Joined: Jun 14, 2011
    Posts: 99

    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

    Joined: Oct 27, 2005
    Posts: 19538
        
      16

    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

    Joined: Jun 14, 2011
    Posts: 99

    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

    Joined: May 03, 2008
    Posts: 4522
        
        5

    You can construct a DefaultComboBoxModel with the array, and setModel(...) on the combo box.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Onchange JCombobox
     
    Similar Threads
    JComboBox inside JTable column
    Is idiot-proofing user input a requirement?
    Dealing with Jlist
    JOptionPane modal for only one window.
    A question about JComboBox