tags:

views:

483

answers:

1

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

A: 

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.

camickr
@camickr...you seem to be an evangelist of SSCCE..:)
Suraj Chandran
posting an SSCCE isn't really possible, as this in the example, myMediaDataAccessor is actually an talking to an RMI object on a remote server, so there is a lot of RMI classes in there. What i would like to know is why with one arraylist, the combobox populates, but not with another, when both lists contain just strings...
joec
Oh and about the accepted rates - i have had literally zero time because i have had so many assignments and projects to do for the end of the week. I do appreciate the answers, just not got round to accepting them yet (!)
joec
@Suraj - Yes, because we are not mind readers and we need all the information to solve a problem. There is no reason that I know of that should cause a combo box to "disappear" just because you change the model. The the poster is doing something strange and it is not a good use of our time to guess what "silly mistake" the poster might be making.
camickr
@joec - if you have time to ask questions, you have time to accept answers. Its that simple! In your case it is easy to create a SSCCE. All you need to do is hard code the data. You already mentioned the data in the array is correct, so you know the problem is not RMI. So the problem must be the way you reset the model of the combo box. That is the whole point of creating a SSCCE, to remove the complex parts of the code and simplify it to see where you are going wrong.
camickr
And if that magically fixes things then you know the problem _WAS_ in the RMI, etc. code and the debugging methodology was flawed. :)
PSpeed
This is where i get slapped wrists! My string was a few characters too long, and the combobox was being pushed off the panel. As soon as i widened my JPanel by a few pixels - it worked fine! Sorry!!
joec
Just proves why a SSCCE is important and shows I gave the proper answer to help solve the problem.
camickr