JavaSwingComponentsRenderers: Renderers


Renderers

quite unconnected

Use

JComboBox: single ListCellRenderer (see JComboBox component adjustments).

JList: single ListCellRenderer

JTree: single TreeCellRenderer

JTableHeader: single default TableCellRenderer or one for each column

JTable: class-based default TableCellRenderers or one for each column

(DefaultTreeCellEditor)

Another place where they would be very useful is JTabbedPane for its tabs (instead of setting 1001 properties individually, and having only a fixed set)

General implementations

* DefaultListCellRenderer (+UIResource, default by BasicListUI, even a UIDefaults property "List.cellRenderer")

* BasicComboBoxRenderer (+UIResource, default by BasicComboBoxUI)

Differences from DefaultListCellRenderer:

JLabel.preferredSize.height fix for null (!) and ""

will not take componentOrientation/enabled

will not show focus border

no performance optimizations

bug: if value is icon will keep old text, if value is text will keep old icon

* MotifComboBoxRenderer (+UIResource, both unused at least since 1.3): similar to BasicComboBoxRenderer, but:

no preferredHeight fix

always sets horizontal alignment to left

* DefaultTableCellRenderer (+UIResource, unused)

* default header renderer (in JTableHeader): subclass of DefaultTableCellRenderer

takes foreground/background/font from header, text from value, border from UIManager, initially set to center horizontal alignment

* unused old default header renderer (in TableColumn) : still the same as in the header

* DefaultTreeCellRenderer (no UIResource; default by BasicTreeUI - broken UIResource pattern: as soon as cellRenderer is changed (to not-null), will not be uninstalled!)

* MotifTreeCellRenderer=DefaultTreeCellRenderer (only contains static icon code), nevertheless used

* WindowsTreeUI.WindowsTreeCellRenderer: non-leaf - displays open icon iff selected (is it that silly in Windows???)

Properties of default renderers

(some redundancy to the text above)

Property

List

Combo

Table

Header

Tree

foreground

from list

from list

(but standard UIs will change)

if set: own value (lost in updateUI)

if not: from table

if editable and focused: always "Table.focusCellForeground"

from header

textNonSelectionColor

("Tree.textForeground", in constructor)

background

from list

from list

(but standard UIs will change)

if set: own value (lost in updateUI)

if not: from table

if editable and focused: always "Table.focusCellForeground"

from header

backgroundNonSelectionColor

("Tree.textBackground", in constructor)

setBackground ignores UIResource fonts (and is ignored if the other background properties are set)

background painting

(i.e. opacity)

enabled (constructor)

enabled (constructor)

1.1.1: enabled (constructor)

1.3: set if table is opaque and has same background (in getTable...)

1.4: true iff table is opaque and has same background (isOpaque overridden)

like Table

disabled, but custom painting code always paints background unles no background color found, and not necessarily under the icon ("Tree.drawsFocusBorderAroundIcon", in constructor)

(#4615385)

setting it to true will break painting

selectionForeground

-> foreground

from list

from list

(but standard UIs will change)

from table

state not supported

textSelectionColor

("Tree.selectionForeground", in constructor)

selectionBackground

-> background

from list

from list

(standard UIs will change)

from table

state not supported

backgroundSelectionColor

("Tree.selectionBackground", in constructor)

setBackground ignores UIResource fonts (and is ignored if the other background properties are set)

border

if focused: "List.focusCellHighlightBorder"

if not: empty border (1, 1, 1, 1)

empty border (1, 1, 1, 1) (constructor)

if focused: "Table.focusCellHighlightBorder"

if not: empty border (1, 1, 1, 1) , used to be (1, 2, 1, 2)

"TableHeader.cellBorder"

not set, but custom painting code paints rectangle in borderSelectionColor ("Tree.selectionBorderColor") by hand (not necessarily around the icon)

font

from list

from list

(but standard UIs will change)

from table

from header

unless set: from tree (by inheritance, 1.4: by overridden getFont (only if not added to tree))

set ignores UIResource fonts

enabled

from list

not set

not set

like Table

from tree

componentOrientation

from list (at least since 1.3)

not set

not set

like Table

from tree (at least since 1.3)

horizontalAlignment

not set

not set

not set

center (construction)

left (in constructor, vs. componentOrientation!)

others

icon or text depending on value (leak was fixed(#4197614))

icon or text depending on value (leaks old)

text

set background/foreground lost in updateUI

text

text

icons from UIManager unless set

"Tree.openIcon", "Tree.closedIcon", "Tree.leafIcon"

UIManager settings not updated in updateUI

low-level painting code depends on exact label painting strategy

null component argument

NullPointerException

NullPointerException

NullPointerException!

old foreground, background, font

NullPointerException

Safe to set from the outside

(for all cells)

horizontal alignment

horizontal alignment

(enabled, componentOrientation)

horizontal alignment

(enabled, componentOrientation)

background, foreground only without updateUI

like Table

(not really accessible)

horizontal alignment

all special properties

Safe to set before getXXXRendererComponent if always

opaque, horizontalAlignment

border

opaque, horizontalAlignment

(enabled, componentOrientation)

foreground, background (only used for non-selected cells)

horizontalAlignment

icon

(enabled, componentOrientation)

n/a

horizontal alignment

all special properties

Safe to set after getXXXRendererComponent even if sometimes

foreground, background

font, border

enabled, componentOrientation

icon, text

foreground, background

font

(but standard UIs will change)

font, border

text

n/a

foreground

icon (set disabled icon if disabled), text

enabled (but update icon), componentOrientation

horizontalAlignment

Safe to set after getXXXRendererComponent (only) if always

opaque, horizontalAlignment

border

enabled, componentOrientation

opaque, horizontalAlignment

icon, text

foreground, background (1.3: update opaque)

enabled, componentOrientation

horizontalAlignment

icon

n/a

special properties expect foreground colors and icons

font (if non-UIResource), border

Unsafe

1.4: opaque (has no effect)

like Table

(not really accessible)

background (will not be used, unless non-UIResource)

font (unless non-UIResource)

opaque (cannot turn off background painting)

selected is cached

I assume that enabled and componentOrientation not being picked up is an oversight and may be fixed somewhen.

No-one can know all these differences. They shouldn't exist. They do mostly because the renderers are the components they return and thus have a large public interface with unclear semantics, and because of some strange re-implementations of some of these methods.

Special implementations

* JTable internal renderers (BooleanRenderer -> JCheckBox; DateRenderer, IconRenderer, NumberRenderer ...)

They are quite weak (Date-/DoubleRenderer always use the default locale (at the first time they were used), not even the JTable's locale; NumberRenderer uses toString, BooleanRenderer doesn't take focus border)

* MetalFileChooserUI:

DetailsTableCellRenderer:

sets horizontal alignment based on column (bug! uses view column, doesn't matter that there is no reordering)

if File value, uses its icon, otherwise null

if File value, uses its text, otherwise null

if Date value, formats it for text (how can it be null if it is an instance of Date?)

otherwise, use toString

DefaultListCellRenderer subclasses:

FileRenderer: text/icon from File, updates external state if selected

DirectoryComboBoxRenderer: text/(special) icon from File ("bug": if null value, does not reset (but icon is always the same anyway))

FilterComboBoxRenderer: text from filter (bug? if null/invalid value, does not reset text)

* MotifFileChooserUI

DefaultListCellRenderer subclasses:

FileRenderer/DirectoryCellRenderer: text from File

FilterComboBoxRenderer = Metal

* WindowsFileChooserUI

DetailsTableCellRenderer: like Metal, changed selection (only file name column and if table focused)

DefaultListCellRenderer subclasses = Metal

Renderers asked without being added to the component hierarchy

tool tip text: JList/JTable/JTableHeader/JTree

JList.updateFixedCellSize <- setCellRenderer, setPrototypeCellValue

TableColumn.sizeWidthToFit

BasicComboPopup: getPopupHeightForRowCount <- getPopupLocation <- show

BasicTableHeaderUI: getHeaderRenderer <- getHeaderHeight <-- get(Minimum|Preferred|Maximum)Size

Accessibility?

Renderers not removed from cell renderer pane

BasicListUI: updateLayoutState <-- getPreferredSize; paint

BasicComboBoxUI: paint

MotifComboBoxUI: paint

MetalComboBoxButton: paint

BasicTreeUI: NodeDimensionsHandler.getNodeDimensions <-- getPreferredSize

Problems of standard renderers

Assumptions (quite strict)

Item states


(C) 2001-2009 Christian Kaufhold (swing@chka.de)