Example usage for javax.swing JList getCellRenderer

List of usage examples for javax.swing JList getCellRenderer

Introduction

In this page you can find the example usage for javax.swing JList getCellRenderer.

Prototype

@Transient
public ListCellRenderer<? super E> getCellRenderer() 

Source Link

Document

Returns the object responsible for painting list items.

Usage

From source file:Main.java

public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JList jlist1 = new JList();
    jlist1.setListData(labels);/*ww w  . j  av a2  s .  c  om*/

    jlist1.setVisibleRowCount(4);
    JScrollPane scrollPane1 = new JScrollPane(jlist1);
    frame.add(scrollPane1, BorderLayout.NORTH);

    ListCellRenderer r = jlist1.getCellRenderer();

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

From source file:Main.java

/**
 * Returns true if the given point is within the actual bounds of the
 * JList item at index (not just inside the cell).
 *///from w  w w . j  a v a  2 s  . c  om
private static boolean pointIsInActualBounds(JList list, int index, Point point) {
    ListCellRenderer renderer = list.getCellRenderer();
    ListModel dataModel = list.getModel();
    Object value = dataModel.getElementAt(index);
    Component item = renderer.getListCellRendererComponent(list, value, index, false, false);
    Dimension itemSize = item.getPreferredSize();
    Rectangle cellBounds = list.getCellBounds(index, index);
    if (!item.getComponentOrientation().isLeftToRight()) {
        cellBounds.x += (cellBounds.width - itemSize.width);
    }
    cellBounds.width = itemSize.width;

    return cellBounds.contains(point);
}

From source file:Main.java

/**
 *
 * @param list/*from  w w  w .  j  av  a2 s  .  com*/
 * @param index
 * @param point
 * @return
 */
public static boolean pointIsInActualBounds(JList list, int index, Point point) {
    ListCellRenderer renderer = list.getCellRenderer();
    ListModel dataModel = list.getModel();
    Object value = dataModel.getElementAt(index);
    Component item = renderer.getListCellRendererComponent(list, value, index, false, false);
    Dimension itemSize = item.getPreferredSize();
    Rectangle cellBounds = list.getCellBounds(index, index);

    if (!item.getComponentOrientation().isLeftToRight()) {
        cellBounds.x += (cellBounds.width - itemSize.width);
    }

    cellBounds.width = itemSize.width;

    return cellBounds.contains(point);
}

From source file:ie.philb.testorderingsoapclient.ui.nav.products.ProductListCellRenderer.java

@Override
public Component getListCellRendererComponent(JList<? extends Product> list, Product value, int index,
        boolean isSelected, boolean cellHasFocus) {

    if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
    } else {//  www  . ja v a 2  s  .  c o m
        if ((index % 2) == 0) {
            //setBackground(list.getBackground());
            setBackground(new java.awt.Color(255, 255, 255));
            setForeground(list.getForeground());
        } else {
            setBackground(new java.awt.Color(245, 245, 245));
        }
    }

    ListModel lm = list.getModel();

    if (lm instanceof ProductListModel) {
        ProductListModel plm = (ProductListModel) lm;

        if (index < plm.getSize()) {
            Product p = plm.getElementAt(index);
            Money unitPrice = p.getUnitPrice();

            lblTitle.setText(WordUtils.capitalize(p.getTitle()));
            lblCode.setText(p.getSkuCode());
            lblUnitPrice.setText("" + unitPrice.getValue().toPlainString());
        } else {
            lblTitle.setText("");
        }

        return this;
    }

    return (Component) list.getCellRenderer();
}