基于另一个JComboBox内容的动态JComboBox内容 [英] Dynamic JComboBox content based on the contents of another JComboBox

查看:83
本文介绍了基于另一个JComboBox内容的动态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来添加到组合框中. /p>

例如:

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

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?

解决方案

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

这篇关于基于另一个JComboBox内容的动态JComboBox内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆