-1

Let's say I want to have a JList like this:

enter image description here

For each rows, there are 2 JLabels: the left is is the color name, and the right one is the color itself. How do you write your own custom ListCellRenderer for this purpose?

anta40
  • 6,511
  • 7
  • 46
  • 73

1 Answers1

3

In outline,

  • Give your JList a ListCellRenderer that displays an Icon, as shown here.

  • Use setHorizontalTextPosition() and setVerticalTextPosition() to achieve the desired layout.

  • Use a custom implementation of Icon, such as the ColorIcon shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045