3

うまくいけば、簡単な質問です。

「カスタムレンダラーの提供」セクションのhttp://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/combobox.htmlの例から、次のようなJComboBoxを作成できます。

Picture 3 - Text 3
-------------------
Picture 1 - Text 1
Picture 2 - Text 2
Picture 3 - Text 3
Picture 4 - Text 4
Picture 5 - Text 5

Picture 3 - Text 3現在選択されているアイテムはどこですか。

カスタムラベルを付けることは可能ですか?そのような

Text 3
-------------------
Picture 1 - Text 1
Picture 2 - Text 2
Picture 3 - Text 3
Picture 4 - Text 4
Picture 5 - Text 5

コンボボックスが最小化された状態のときに画像が表示されない場合。

以前、これをエミュレートするためにJButton /装飾されていないポップアップJFrameを使用しましたが、純粋なJComboBoxで実行できるかどうか疑問に思っています。

ありがとう

4

2 に答える 2

4

カスタムラベルを付けることは可能ですか?そのような...

はい。同じレンダラーを使用して、ドロップダウンリストとコンボボックス内の選択したアイテムをレンダリングします。「レンダラーインデックス」が-1の場合、選択された値はレンダラーであるため、必要に応じてレンダリングをカスタマイズできます。何かのようなもの:

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

public class ComboBoxItemIcon extends JFrame
{
    public ComboBoxItemIcon()
    {
        Vector model = new Vector();
        model.addElement( new Item(new ImageIcon("copy16.gif"), "copy" ) );
        model.addElement( new Item(new ImageIcon("add16.gif"), "add" ) );
        model.addElement( new Item(new ImageIcon("about16.gif"), "about" ) );

        JComboBox comboBox;

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

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

            Item item = (Item)value;

            if (index == -1)
            {
                setText( item.getText() );
                setIcon( null );
            }
            else
            {
                setText( item.getText() );
                setIcon( item.getIcon() );
            }

            return this;
        }
    }

    class Item
    {
        private Icon icon;
        private String text;

        public Item(Icon icon, String text)
        {
            this.icon = icon;
            this.text = text;
        }

        public Icon getIcon()
        {
            return icon;
        }

        public String getText()
        {
            return text;
        }
    }

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

}
于 2010-07-27T02:15:57.177 に答える
1

画像とテキストの表示に関連するレンダリングには、次の3つの関数呼び出しがあるようです。

setIcon setText setFont

この例はコンパイルしていませんが、setIcon(icon);をコメントアウトしてみます。関数getListCellRendererComponentから。これは、選択したアイテムの画像を表示しているように見えるためです。

コメントアウトするとコードが壊れてしまう場合は、回避策として空白の画像などでオーバーライドしてみます。

于 2010-07-26T21:59:03.950 に答える