Skip to main content

Wildcards in the Wild

Posted by cayhorstmann on December 13, 2012 at 10:31 AM PST

Nothing new here...just keep moving. I refreshed an older blog to fix some awful formatting issues that the blogging system introduced when deciding to convert all &lt; to <, which makes any HTML document about generics a bit hard to read :-)

People kvetch about wildcards

Related Topics >>


I think the secret is that comboBox.getRenderer() should ...

I think the secret is that comboBox.getRenderer() should return a ListCellRenderer<E>. There is no way of getting the original type back out of the JComboBox<E> class, but you do know that any ListCellRenderer you fed to that class can at least render Es. This should be enough type information to implement localeRenderer.

In Scala, we'd define the ListCellRenderer class as ListCellRenderer[+E], which means the class is covariant. The Scala type system treats ListCellRenderer[Locale] as a subclass of ListCellRenderer[Object], and you wouldn't need to add wildcards on the JComboBox class.