Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java ComboBox not updating

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Java ComboBox not updating

    I'm trying to update a ComboBox by adding new items to a model.

    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
    			for (int i = 0; i < values.length; i++)
    			{
    				model.addElement(values[i]);
    			}
    			jComboBox.setModel(model);

    This should update the items, but it's only updating the size of the ComboBox. I'm not able to select any of the newly added items.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,079
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Java ComboBox not updating

    but it's only updating the size of the ComboBox
    That doesn't make sense. Why would the size change independent of the contents? Are you sure that the array values[] contains the desired items?

    Can you post a short example that demonstrates the problem?

    Edit: If you're adding items to those already existing in the JComboBox, you should start with the existing model and add to that.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java ComboBox not updating

    Yes, I'm using an already existing JComboBox, but adding it directly to it has the same effect. The model I'm getting with getModel() has no add method.

    Here's a screenshot that proves the problem:

    problem.jpg

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,079
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: Java ComboBox not updating

    Here's an example of an if statement in a JComboBox change listener I wrote that does what you've described. Let me know if you have questions or would like to see the if statement in action. Perhaps you could show your code in action first.
    // selectedIndex is the selected index of the JComboBox being watched
    // by the listener. the program varies a second JComboBox based on
    // the selection in the first JComboBox
    if ( selectedIndex >= 1 && selectedIndex < 5 )
    {
        // obtain the second JComboBox' model to modify its contents
        DefaultComboBoxModel<String> model = 
                (DefaultComboBoxModel)comboBox2.getModel();
     
        // remove all elements
        model.removeAllElements();
     
        // magic number 4 is 4 choices per index
        for ( int i = 0 ; i < selectedIndex * 4 ; i++ )
        {
            model.addElement( "" + (char)( 'A' + i ) );
        }
    }

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Bingo90 (March 30th, 2014)

  6. #5
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java ComboBox not updating

    I just found out what my problem was. I had 2 jComboBox fields (One in the class and one in the main) and added the wrong one to the frame.

Similar Threads

  1. [SOLVED] Java AWT Graphics2D - Not Updating Sprites
    By Max707 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 15th, 2012, 07:08 AM
  2. updating Java Objects(Longblob) in mysql databasel problem
    By chronoz13 in forum JDBC & Databases
    Replies: 5
    Last Post: February 12th, 2012, 04:07 AM
  3. [SOLVED] Populating second combobox from a combobox help!
    By ComputerSaysNo in forum AWT / Java Swing
    Replies: 7
    Last Post: October 18th, 2011, 09:01 AM
  4. updating SWT Slider from separate java class
    By ppmckee in forum AWT / Java Swing
    Replies: 0
    Last Post: April 14th, 2011, 03:23 PM
  5. Need Help on Updating a GUI Java
    By coyboss in forum Object Oriented Programming
    Replies: 9
    Last Post: March 6th, 2011, 09:00 PM

Tags for this Thread