Wolfish
Wolfish

Reputation: 970

Dynamic JComboBox content based on the contents of another JComboBox

This code is sloppy, I would also welcome some feedback on it.

I'm trying to change the value of a JComboBox based on the value of another JComboBox. There is an additional complication in that I'm using an additional class to determine what array of strings to return (see my previous question).

In theory, the code I have should work:

    String[] siteSelectStrings = {"Site", "London", "Long Island"};
    JComboBox regSiteSelectBox = new JComboBox(siteSelectStrings);
    regSiteSelectBox.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent arg0) {
            getBuildingList gbl = new getBuildingList();
            regBuildingSelectBox.addItem(gbl.buildingSelectList((String)(regSiteSelectBox.getSelectedItem())));
            }
        });
    regSiteSelectBox.setBounds(24, 336, 282, 20);
    contentPane.add(regSiteSelectBox);


    regBuildingSelectBox = new JComboBox();
    regBuildingSelectBox.setBounds(24, 367, 282, 20);
    contentPane.add(regBuildingSelectBox);

And the method for returning the Building array:

public class getBuildingList {

public String[] buildingSelectList(String site)
{
    switch (site)
    {
    case "London":
        return new String[]  {"Building", "Harvell", "LYNX Complex", "Caroline", "Salters"};
    case "Long Island":
        return new String[] {"Building", "Phillips", "Pascal"};
    }
    return new String[] {"Failed to populate buildings"};
    }  
}

But, instead of returning a legible string, it returns the following:

[Ljava.lang.String;@917081d

I have no idea how to decode that, although it seems like a memory reference. Where am I going wrong?

Upvotes: 0

Views: 287

Answers (2)

camickr
camickr

Reputation: 324108

If you method is returning an Array of Strings to be displayed in the combo box, then you need to create a new ComboBoxModel to add to the combo box.

For example:

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

public class ComboBoxTwo extends JPanel implements ActionListener
{
    private JComboBox<String> mainComboBox;
    private JComboBox<String> subComboBox;
    private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();

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

        //  prevent action events from being fired when the up/down arrow keys are used
        mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        add( mainComboBox );

        //  Create sub combo box with multiple models

        subComboBox = new JComboBox<String>();
        subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        add( subComboBox );

        JButton arrow = SwingUtils.getDescendantOfType(JButton.class, subComboBox, "Text", "");
        Dimension d = arrow.getPreferredSize();
        System.out.println(arrow.getClass());
        System.out.println(d);
        d.width = 100;
        arrow.setPreferredSize(d);

        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 ) );
        }
    }

    private static void createAndShowUI()
    {
        try
        {
//          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) { }
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new ComboBoxTwo() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Upvotes: 2

Raven
Raven

Reputation: 3516

Okay as far as I can see it your problem is that you are adding the complete array of Strings as one item. The JCombobox then converts it to a single String by invoking the toString() mehtod which causes it to display [Ljava.lang.String;@917081d.
In order for the content of your array to be displayed as single entries in the JComboBox you have to clear it and then add each item seperately:

regBuildingSelectBox.removeAllItems();

for(String currentEntry : gbl.buildingSelectList((String)(regSiteSelectBox.getSelectedItem())) {  
        regBuildingSelectBox.addItem(currentEntry);
}

And as you asked for additional feedback on your code... I'd suggest using enums instead of hard coded String arrays. That's just a potential error source

Upvotes: 1

Related Questions