10

addItem()コレクションのすべての要素をJComboBox (を使用) に設定しています。コレクション内の各要素はHashMap(ハッシュマップの ComboBox です..) です。

私の質問は - 各項目をHashMapGUI のコンボボックスに表示するようにテキストを設定するにはどうすればよいですか? マップ内の特定のキーの値である必要があります。通常、コンボボックスに独自のタイプを入力する場合は、toString()メソッドをオーバーライドします...しかし、Java HashMap を使用しているため、これを達成する方法がわかりません。

アイデアはありますか (可能であれば、独自の HashMap を実装せずに)?

更新: カスタム機能が必要な場合は、オブジェクトを JComboBox オーバーライド toString() にすることを避ける方法はないようです..(1) JComboBox にロードするオブジェクトを指定し、( 2) これらのオブジェクトが GUI でどのように表示されるかを指定します。

4

3 に答える 3

11

(2) これらのオブジェクトが GUI でどのように表示されるかを指定します。

モデルに任意のオブジェクトを追加し、カスタム レンダラーを作成して、任意の方法でオブジェクトを表示できます。toString() アプローチとカスタム レンダラー アプローチを示す簡単な例:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxItem extends JFrame implements ActionListener
{
    public ComboBoxItem()
    {
        Vector model = new Vector();
        model.addElement( new Item(1, "car" ) );
        model.addElement( new Item(2, "plane" ) );
        model.addElement( new Item(3, "train" ) );
        model.addElement( new Item(4, "boat" ) );

        JComboBox comboBox;

        //  Easiest approach is to just override toString() method
        //  of the Item class

        comboBox = new JComboBox( model );
        comboBox.setDragEnabled(true);
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.NORTH );

        //  Most flexible approach is to create a custom render
        //  to diplay the Item data

        comboBox = new JComboBox( model );
        comboBox.setDragEnabled(true);
        comboBox.setRenderer( new ItemRenderer() );
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.SOUTH );
    }

    public void actionPerformed(ActionEvent e)
    {
        JComboBox comboBox = (JComboBox)e.getSource();
        Item item = (Item)comboBox.getSelectedItem();
        System.out.println( item.getId() + " : " + item.getDescription() );
    }

    class ItemRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index,
                isSelected, cellHasFocus);

            if (value != null)
            {
                Item item = (Item)value;
                setText( item.getDescription().toUpperCase() );
            }

            if (index == -1)
            {
                Item item = (Item)value;
                setText( "" + item.getId() );
            }


            return this;
        }
    }

    class Item
    {
        private int id;
        private String description;

        public Item(int id, String description)
        {
            this.id = id;
            this.description = description;
        }

        public int getId()
        {
            return id;
        }

        public String getDescription()
        {
            return description;
        }

        public String toString()
        {
            return description;
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxItem();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
     }

}
于 2010-05-11T17:49:23.400 に答える
0

メソッドをオーバーライドする場合は、を実装および使用して必要なメソッドを実装し、独自の実装を提供するtoString()デコレータクラスを作成するだけです。MapHashMaptoString()

于 2010-05-11T17:16:39.757 に答える
0

ハッシュマップがある場合は、次のようなことを行う必要があります。

JComboBox box = new JComboBox(hashMap.getValues().toArray());

もちろん、あなたはあなたが持ってtoStringいるオブジェクトのメソッドをオーバーライドする必要がありますHashMap

于 2010-05-11T17:18:12.670 に答える