动态JComboBox内容基于另一个JComboBox的内容

时间:2016-12-04 12:15:17

标签: java swing jcombobox

这段代码很草率,我也欢迎一些反馈。

我试图根据另一个JComboBox的值更改JComboBox的值。还有一个复杂的问题是,我使用一个额外的类来确定要返回的字符串数组(参见我之前的问题)。

理论上,我的代码应该有效:

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

返回Building数组的方法:

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

但是,它不是返回一个易读的字符串,而是返回以下内容:

[Ljava.lang.String;@917081d

我不知道如何解码它,虽然它看起来像是一个内存引用。我哪里错了?

2 个答案:

答案 0 :(得分:2)

如果您的方法返回要在组合框中显示的字符串数组,则需要创建一个新的ComboBoxModel以添加到组合框中。

例如:

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

答案 1 :(得分:1)

好的,就我所知,你的问题是你将完整的Strings数组添加为一个项目。 JCombobox然后通过调用toString() mehtod将其转换为单个String,使其显示[Ljava.lang.String;@917081d
为了使您的数组内容在JComboBox中显示为单个条目,您必须清除它,然后单独添加每个项目:

regBuildingSelectBox.removeAllItems();

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

当您要求提供有关代码的其他反馈时...我建议使用枚举而不是硬编码的String数组。这只是一个潜在的错误来源