Results 1 to 4 of 4
  1. #1
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    23
    Rep Power
    0

    Unhappy Mirrored JComboBox

    I need to create 5 comboboxes that all refer to a master combobox. I thought that having created one and populating it...

    JComboBox cb1 = new JComboBox();

    after a suitable code to populate the cb1

    I thought could simply make another mirror of it by

    JComboBox cb2 = new JComboBox(cb1);

    I used Netbeans GUI builder to produce the JComboBoxes on a form, populating the master and then pointed the others at the master...or so I thought, but when I run the programme the master is ok but the others are empty

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default Re: Mirrored JComboBox

    Read the API documentation for the JComboBox class: it doesn't have a constructor that takes another JComboBox as a single parameter; you were just hoping and assuming that the (incorrect) code would do what you intended; always first read the API documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    23
    Rep Power
    0

    Default Re: Mirrored JComboBox

    Thanks Jos, you are 100% right about the 'hoping' element, so how do I go about removing (or adding/changing) an item in one combobox and ensuring that all of the others reflect the change?
    Is it a case of
    1. making the change
    2. converting the model of that cb to a String[]
    3. use removeAll() on the remainder
    4. reloading the others from that String[]?

    I thought that combobox being an object meant that if
    a. JComboBox cb1 = new JComboBox();
    b. it was suitably loaded with its dropdown content then
    c. JComboBox cb2 = new JComboBox(); ( a new and separate object at the moment and in a different position on the form)
    d. then cb2 = cb1; would make cb2 point to and effectively be identical to cb1 even though it occupied a different position

    I have a long way to go yet but at 75 years young it is keeping my brain active...

    Thanks again
    Gordon

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default Re: Mirrored JComboBox

    If both (or more) combo boxes need to have the same content, they can share one single ComboBoxModel (read the API docs again). Changes in that one single model are 'propagated' to all the 'attached' JComboBoxes. Otherwise you have to do it the hard way: if one combo box (it's model) changes you have to rebuild the models for the other combo boxes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. JComboBox!!!
    By hassaanid2012 in forum AWT / Swing
    Replies: 1
    Last Post: 06-05-2012, 09:58 PM
  2. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  3. JComboBox
    By pinks_70986 in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 05:26 PM
  4. I need help with JComboBox
    By eva21 in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 10:05 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •