1

productComboBoxGUIに2 つの JComboBox コンポーネントを追加し、categoryComboBoxそれぞれに次のアイテム リスナーを定義します。

    categoryComboBox.addItemListener(new GoItemListener());
    productComboBox.addItemListener(new ProductItemListener());

ユーザーが最初に製品を選択すると、リスナーは選択された製品に応じてカテゴリ ボックスにデータを入力する必要があります。私の項目リスナーは内部クラスです。

ProductItemListenerpopulateCategories次のようなメソッドを呼び出します。

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

私の にはproductComboBox、ミュージックとビデオの 2 つの商品アイテムがあります。Music を選択するcategoryComboBoxと、ArrayList の文字列が正しく入力されます。

問題は、ビデオを選択すると、categoryArrayList文字列の正しい ArrayList が含まれているため、categoryComboBox例外が発生しないため、データが返されてに追加されているように見えることですcategoryComboBox。GUI から消えるだけです。

何か案は?
ありがとう

4

1 に答える 1

1

あなたが投稿したランダムなコードに基づいて、あなたが何をしているのかを推測するのは難しく、25% の受け入れ率を考えると、あなたが得た提案に感謝していないように見える場合、私は答えるべきかどうかわかりませんでした.

とにかく、これは私が2つの関連するコンボボックスを共有する方法です:

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

さらにヘルプが必要な場合は、問題を示すSSCCEを投稿してください。

于 2009-12-11T00:52:04.167 に答える