Add your own ListCellRenderer : JList Renderer « Swing « Java Tutorial






For performance reasons, do not create the renderer in the getListCellRendererComponent() method.

Add your own ListCellRenderer
import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class SizingSamples {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(100);
    for (int i = 0; i < 100; i++) {
      model.addElement(Integer.toString(i));
    }
    JList jlist2 = new JList(model);
    ListCellRenderer renderer = new FocusedTitleListCellRenderer();
    jlist2.setCellRenderer(renderer);

    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);

    frame.setSize(300, 350);
    frame.setVisible(true);

    jlist2.ensureIndexIsVisible(50);
  }
}

class FocusedTitleListCellRenderer implements ListCellRenderer {
  protected static Border noFocusBorder = new EmptyBorder(15, 1, 1, 1);

  protected static TitledBorder focusBorder = new TitledBorder(LineBorder.createGrayLineBorder(),
      "title");

  protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

  public Component getListCellRendererComponent(JList list, Object value, int index,
      boolean isSelected, boolean cellHasFocus) {
    JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,
        isSelected, cellHasFocus);
    renderer.setBorder(cellHasFocus ? focusBorder : noFocusBorder);
    return renderer;
  }
}








14.41.JList Renderer
14.41.1.Rendering JList Elements
14.41.2.Install List cell rendererInstall List cell renderer
14.41.3.Add your own ListCellRendererAdd your own ListCellRenderer
14.41.4.Creating a Complex ListCellRenderer: font, icon and colorCreating a Complex ListCellRenderer: font, icon and color
14.41.5.The items can be arbitrary objects. The toString() method of the objects is displayed in the list component.
14.41.6.Setting the Dimensions of an Item in a JList Component
14.41.7.Set the item dimensions using a sample value
14.41.8.Change the layout orientation so that its items are displayed top-to-bottom and left-to-right.