0

シナリオは次のとおりです。

データベースから値を取得する2 つの JComboBox (combo1 およびombo2 と呼びます) があります[DB では、これら 2 つは 1:M の関係にあります]。画面が表示されたら、combo1 にデータベースの値を入力し、リストの最初のエントリを取得して対応する値を取得し、combo2 に入力します。

コンボ 2 の値はコンボ 1 で選択されているものに依存するため、コンボ 1 で選択が変更されるたびに、データベースに対して呼び出しが行われ、一致する値を取得してコンボ 2 に入力されます。

ここに問題があります:

コンボ 1 に 2 つのエントリがあり、2 番目のエントリにはコンボ 2 に対応する値がないとします。コンボ 1 の 2 番目のエントリを選択すると、コンボ 2 で最後に選択した値がクリアされません。[コンボ 2 のモデルは空であるため、何も選択しないでください]

質問: モデルが空の場合、combo2 のテキストをクリアするにはどうすればよいですか?

サンプルコードは次のとおりです。

public void select(final Entry entry) {
      if (entry == null)
         return;

         int index = entryList.indexOf(entry); // instance of SelectionInList from JGoodies
         boolean positive = index >= 0 && index <= entryList.getSize() - 1;

         if (positive) {

              entryList.setSelection(entry);


               subEntryList.setList(entryList.loadSubEntries(entry.getID()));
               if (!subEntryList.isEmpty()) {
                    SubEntry e = subEntryList.getElementAt(0);
                    select(e);
                 }
        }

}

4

3 に答える 3

1

空のコンボボックス モデルがある場合、ビューは自動的にクリアされます。DefaultComboBoxModel.fireIntervalRemoved()独自のモデルを派生させた場合は、エントリを削除する場合に呼び出すことを忘れないでください。

別の (この場合はお勧めしません) 方法は、 を使用することcombobox.setSelectedItem(null);です。

于 2011-04-09T12:26:54.807 に答える
0

最初のコンボボックスで選択を行うときに、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 );

        //  prevent action events from being fired when the up/down arrow keys are used
//      mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        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);
//      mainComboBox.setSelectedIndex(1);
    }

    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 );
     }
}
于 2011-04-09T15:02:21.030 に答える