I attempted to use the following without success.
comboBoxMaxFileSize.setRenderer(new DateRenderer());
static class DateRenderer extends DefaultListCellRenderer {
NumberFormat formatter;
public DateRenderer() { super(); }
public void setValue(Object value) {
if (formatter==null) {
formatter = new DecimalFormat("#,##0");
}
setText((value == null) ? "" : formatter.format(value));
}
}
I had previously tried the following, without success:
private static class NumberFormatListRenderer implements ListCellRenderer{
public Component getListCellRendererComponent(
JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
try{
Integer.parseInt(value.toString());
JFormattedTextField field =
new JFormattedTextField(new DecimalFormat("#,##0"));
field.setValue(value);
return field;
}catch (Exception e){
return new JTextField(value.toString());
}
}
}
The following seems to work, but it only displays the selection when the dropdown menu is in place. Once you select something, nothing is displayed in the combobox selection area.
private static class NumberFormatListRenderer implements ListCellRenderer{
public Component getListCellRendererComponent(
JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
try{
int val = Integer.parseInt(value.toString());
return new JLabel(
new DecimalFormat("#,###,###,##0").format(val));
}catch (Exception e){
return new JLabel(value.toString());
}
}
}
I used the code you provided in reply 2 except I overrode the getListCellRendererComponent() method instead of the setValue() method and added a super.getListCellRendererComponent(...) call at the beginning. I also changed the data to be stored as an Integer insead of a String. It doesn't make sense to change the String to and Integer every time you render the cell.
class MyRenderer extends BasicComboBoxRenderer
{
NumberFormat formatter;
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (formatter == null)
{
formatter = new DecimalFormat("#,##0");
setHorizontalAlignment(JLabel.RIGHT);
}
setText((value == null) ? "" : formatter.format(value));
return this;
}
}
FYI: This is the final class I came up with. It deals with cases where not all elements in the list are Numbers and also the case where items are not Numbers but their string representation is parsable. (I have a case where I have an enum that has both of the above issues)
public class FormattedNumberComboBoxRenderer extends BasicComboBoxRenderer {
private static final NumberFormat formatter = new DecimalFormat("#,##0.#");
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);
try{
double val = Double.parseDouble(value.toString());
setHorizontalAlignment(JLabel.RIGHT);
setText((value == null) ? "" : formatter.format(val));
}
catch (Exception e){}
return this;
}
}