目录搜索
文字
分享


JavaTM 2 Platform
Standard Ed. 6

javax.swing
接口 ListCellRenderer

所有已知实现类:
BasicComboBoxRenderer, BasicComboBoxRenderer.UIResource, DefaultListCellRenderer, DefaultListCellRenderer.UIResource, MetalFileChooserUI.FileRenderer, MetalFileChooserUI.FilterComboBoxRenderer

1

public interface <b>ListCellRenderer</b>

标识可用作“橡皮图章”以绘制 JList 中单元格的组件。例如,要将 JLabel 用作 ListCellRenderer,则需要编写:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

class MyCellRenderer extends JLabel implements ListCellRenderer {

    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;

    }

}

另请参见:
JList, DefaultListCellRenderer

方法摘要
 Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
          返回已配置用于显示指定值的组件。
 

方法详细信息

getListCellRendererComponent

1

2

3

4

5

Component <b>getListCellRendererComponent</b>(JList list,

                                       Object value,

                                       int index,

                                       boolean isSelected,

                                       boolean cellHasFocus)

返回已配置用于显示指定值的组件。然后调用该组件的 paint 方法来“呈现”单元格。如果由于列表单元格没有固定的大小而有必要计算该列表的尺寸,则调用此方法来生成一个可在其上调用 getPreferredSize 的组件。

参数:
list - 正在绘制的 JList。
value - 由 list.getModel().getElementAt(index) 返回的值。
index - 单元格索引。
isSelected - 如果选择了指定的单元格,则为 true。
cellHasFocus - 如果指定的单元格拥有焦点,则为 true。
返回:
其 paint() 方法将呈现指定值的组件。
另请参见:
JList, ListSelectionModel, ListModel

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。