インタフェースListCellRenderer<E>

型パラメータ:
E - このレンダラが使用できる値のタイプ
既知のすべての実装クラス:
BasicComboBoxRenderer, BasicComboBoxRenderer.UIResource, DefaultListCellRenderer, DefaultListCellRenderer.UIResource, MetalFileChooserUI.FileRenderer, MetalFileChooserUI.FilterComboBoxRenderer

public interface ListCellRenderer<E>
JList内のセルをペイントする「ゴム印」として使用できるコンポーネントを識別します。 たとえば、JLabelをListCellRendererとして使用するには、次のように書きます。

class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
    public MyCellRenderer() {
        setOpaque(true);
    }

    public Component getListCellRendererComponent(JList<?> list,
                                                  Object value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus) {

        setText(value.toString());

        Color background;
        Color foreground;

        // check if this cell represents the current DnD drop location
        JList.DropLocation dropLocation = list.getDropLocation();
        if (dropLocation != null
                && !dropLocation.isInsert()
                && dropLocation.getIndex() == index) {

            background = Color.BLUE;
            foreground = Color.WHITE;

        // check if this cell is selected
        } else if (isSelected) {
            background = Color.RED;
            foreground = Color.WHITE;

        // unselected, and not the DnD drop location
        } else {
            background = Color.WHITE;
            foreground = Color.BLACK;
        };

        setBackground(background);
        setForeground(foreground);

        return this;
    }
}

導入されたバージョン:
1.2
関連項目:
  • メソッドのサマリー

    修飾子と型
    メソッド
    説明
    getListCellRendererComponent(JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus)
    指定された値を表示するように設定されたコンポーネントを返します。
  • メソッドの詳細

    • getListCellRendererComponent

      Component getListCellRendererComponent(JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus)
      指定された値を表示するように設定されたコンポーネントを返します。 次に、そのコンポーネントのpaintメソッドが呼び出されて、セルを「レンダリング」します。 リスト・セルのサイズが固定されていないため、リストの寸法を計算する必要がある場合には、このメソッドが呼び出されて、getPreferredSizeを呼び出せるコンポーネントを生成します。
      パラメータ:
      list - ペイントしているJList。
      value - list.getModel().getElementAt(index)によって返される値。
      index - セルのインデックス。
      isSelected - 指定されたセルが選択された場合はtrue。
      cellHasFocus - 指定されたセルにフォーカスがある場合はtrue。
      戻り値:
      指定された値をレンダリングするpaint()メソッドがあるコンポーネント
      関連項目: