extends AbstractGCellRenderer implements ListC
*
* Use this if you only need to provide a way to get the string value from the type being shown
* in the list.
- *
+ *
* @param cellToTextMappingFunction a function that maps your custom type to a string value
* @return new GListCellRenderer instance
*/
@@ -75,6 +74,7 @@ public class GListCellRenderer extends AbstractGCellRenderer implements ListC
boolean isSelected, boolean hasFocus) {
setText(getItemText(value));
+
setHorizontalAlignment(
value instanceof Number ? SwingConstants.RIGHT : SwingConstants.LEFT);
ListModel extends E> model = list.getModel();
@@ -83,14 +83,13 @@ public class GListCellRenderer extends AbstractGCellRenderer implements ListC
if (isSelected) {
setForeground(list.getSelectionForeground());
setBackground(list.getSelectionBackground());
- setOpaque(true);
}
else {
setForegroundColor(list, model, value);
JList.DropLocation dropLocation = list.getDropLocation();
// @formatter:off
- boolean isDropRow = (dropLocation != null &&
+ boolean isDropRow = (dropLocation != null &&
dropLocation.isInsert() &&
dropLocation.getIndex() == index);
// @formatter:on
@@ -103,6 +102,7 @@ public class GListCellRenderer extends AbstractGCellRenderer implements ListC
}
setBorder(hasFocus ? focusBorder : noFocusBorder);
+
return this;
}
@@ -114,30 +114,4 @@ public class GListCellRenderer extends AbstractGCellRenderer implements ListC
protected void configureFont(JList extends E> list, ListModel extends E> model, int index) {
setFont(defaultFont);
}
-
- /**
- * Returns the width, height necessary to display the largest element in this list.
- *
- * Useful for setting a JList's fixed cell width and height to the actual necessary size.
- *
- * NOTE: the items and the renderer must be in plain text mode, not HTML rendering mode.
- *
- * @param list the JList that uses this cell renderer
- * @param items the items to measure
- * @param minWidth the minimum width that can be returned
- * @param minHeight the minimum height that can be returned
- * @return a new Dimension containing a width and height value necessary to display the largest
- * element in the list
- */
- public Dimension computePlainTextListCellDimensions(JList extends E> list, List items,
- int minWidth, int minHeight) {
- configureFont(list, list.getModel(), 0);
- FontMetrics metrics = getFontMetrics(getFont());
- int maxWidth = minWidth;
- for (E item : items) {
- String text = getItemText(item).toString();
- maxWidth = Math.max(maxWidth, metrics.stringWidth(text));
- }
- return new Dimension(maxWidth, Math.max(metrics.getHeight(), minHeight));
- }
}