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

标签 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

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

最佳答案

如果您的方法返回要在组合框中显示的字符串数组,那么您需要创建一个新的 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();
            }
        });
    }
}

关于java - 基于另一个 JComboBox 内容的动态 JComboBox 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40958505/

相关文章:

java - 使用 java 获取 Watson 对话的所有输出

java - 设置列标题的最大高度 - NatTable

java - Spring 启动错误: Use of @OneToMany or @ManyToMany targeting an unmapped class

java - 在 Swing 中重新定位 GridLayout

java - Java中SocketChannel的SSL实现

java - 将文本区域的内容发送到打印机

java - getClipBounds 与 JScrollPane 结合使用错误

java - 使用来自 sql 的数据将数据添加到组合框 - java

java - JComboBox 的自定义字体

java - JRadioButton 项目监听器未触发