Hi,everybody
As the title, is there any methods?
I just tried, but no finding.
regards,
poorrichard
Roland de Ruiter - 27 Feb 2006 11:31 GMT
> Hi,everybody
>
[quoted text clipped - 3 lines]
> regards,
> poorrichard
A ListCellRenderer takes care of rendering the combobox items. Class
BasicComboBoxRenderer (the default implementation of the
ListCellRenderer interface) extends JLabel. For right alignment, you
could try the following code
<untested>
JComboBox yourComboBox = new JComboBox();
ListCellRenderer itemRenderer = yourComboBox.getRenderer();
if (itemRenderer instanceof JLabel) {
JLabel itemRendererLabel = (JLabel)itemRenderer;
itemRendererLabel.setHorizontalAlignment(SwingConstants.RIGHT);
}
</untested>
Regards,
Roland
ph@semm.tmfweb.nl - 27 Feb 2006 12:13 GMT
This is what I could think of.
cb.setRenderer(new MyCellRenderer());
.....
class MyCellRenderer extends JLabel implements ListCellRenderer {
Color bg;
public MyCellRenderer() {
super("", JLabel.RIGHT );
setOpaque(true);
bg = this.getBackground();
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
setText(value.toString());
setBackground(isSelected ? Color.YELLOW : bg );
return this;
}
}
Color handling could be improved.
Anyone know an easier/shorter way ?
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
Andrey Kuznetsov - 27 Feb 2006 14:00 GMT
> Color handling could be improved.
> Anyone know an easier/shorter way ?
probably you may extend DefaultListCellRenderer instead.

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
poorichard@gmail.com - 28 Feb 2006 01:01 GMT
Dear people,
I tried your method, and it works.
Or to implements ListCellRenderer .
thinks all,
Richard