joec
joec

Reputation: 3543

JComboBox adds items for one ArrayList of strings, but disappears from GUI for another

I have 2 JComboBox components added to my GUI productComboBox and categoryComboBox, with the following item listeners defined for each:

    categoryComboBox.addItemListener(new GoItemListener());
    productComboBox.addItemListener(new ProductItemListener());

The user first selects a product, and then the listener should populate the category box dependent on which product is selected. My item listeners are inner classes.

ProductItemListener calls a method populateCategories which looks like this:

    protected void populateCategories() {
        String product = productComboBox.getSelectedItem().toString();

        myMediaDataAccessor.init(product);

        ArrayList categoryArrayList = null;
        categoryArrayList = myMediaDataAccessor.getCategories();

        Iterator iterator = categoryArrayList.iterator();
        String aCategory;
        while (iterator.hasNext()) {
            aCategory = (String) iterator.next();
            categoryComboBox.addItem(aCategory.toString());
        }
    }

I have two product items in my productComboBox, Music and Videos. If I select Music then my categoryComboBox gets populated correctly with the strings from the ArrayList.

The problem is, if i select Videos, my categoryArrayList contains the correct ArrayList of strings, so my data is being returned and seemingly added to the categoryComboBox as I'm not getting any exceptions, its just that my categoryComboBox disappears from the GUI.

Any ideas?
Thanks

Upvotes: 1

Views: 13954

Answers (1)

camickr
camickr

Reputation: 324128

Based on the random code you posted its hard to guess what you are doing and given you 25% accepted rate I wasn't sure if I should answer when you don't appear to appreciate the suggestions you get.

Anyway, this is how I share two related combo boxes:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ComboBoxTwo extends JFrame implements ActionListener
{
    private JComboBox mainComboBox;
    private JComboBox subComboBox;
    private Hashtable subItems = new Hashtable();

    public ComboBoxTwo()
    {
        String[] items = { "Select Item", "Color", "Shape", "Fruit" };
        mainComboBox = new JComboBox( items );
        mainComboBox.addActionListener( this );

        getContentPane().add( mainComboBox, BorderLayout.WEST );

        //  Create sub combo box with multiple models

        subComboBox = new JComboBox();
        subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        getContentPane().add( subComboBox, BorderLayout.EAST );

        String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };
        subItems.put(items[1], subItems1);

        String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };
        subItems.put(items[2], subItems2);

        String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };
        subItems.put(items[3], subItems3);
    }

    public void actionPerformed(ActionEvent e)
    {
        String item = (String)mainComboBox.getSelectedItem();
        Object o = subItems.get( item );

        if (o == null)
        {
            subComboBox.setModel( new DefaultComboBoxModel() );
        }
        else
        {
            subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxTwo();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
     }
}

If you need more help post your SSCCE that shows the problem.

Upvotes: 1

Related Questions