java JComboBox 为一个字符串 ArrayList 添加项目,但从另一个 GUI 中消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1885126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JComboBox adds items for one ArrayList of strings, but disappears from GUI for another
提问by joec
I have 2 JComboBox components added to my GUI productComboBoxand categoryComboBox, with the following item listeners defined for each:
我有 2 个 JComboBox 组件添加到我的 GUIproductComboBox和categoryComboBox,并为每个组件定义了以下项目侦听器:
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.
用户首先选择产品,然后侦听器应根据选择的产品填充类别框。我的项目侦听器是内部类。
ProductItemListenercalls a method populateCategorieswhich looks like this:
ProductItemListener调用一个populateCategories看起来像这样的方法:
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 categoryComboBoxgets populated correctly with the strings from the ArrayList.
我的productComboBox音乐和视频中有两个产品项目。如果我选择 Music,那么我categoryComboBox的 ArrayList 中的字符串会被正确填充。
The problem is, if i select Videos, my categoryArrayListcontains the correct ArrayList of strings, so my data is being returned and seemingly added to the categoryComboBoxas I'm not getting any exceptions, its just that my categoryComboBoxdisappears from the GUI.
问题是,如果我选择视频,我的categoryArrayList包含正确的字符串 ArrayList,所以我的数据被返回并似乎添加到了,categoryComboBox因为我没有得到任何异常,只是我的categoryComboBox从 GUI 中消失了。
Any ideas?
Thanks
有任何想法吗?
谢谢
采纳答案by camickr
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.
根据您发布的随机代码,很难猜测您在做什么,并且给了您 25% 的接受率,我不确定当您似乎不欣赏您得到的建议时我是否应该回答。
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 SSCCEthat shows the problem.
如果您需要更多帮助,请发布显示问题的SSCCE。

