Formatting numbers is combobox

Is there an example of formatting numbers in a combo box? I have a combobox with a list of large numbers. I would like the numbers displayed in #,### format.Thanks,John
[189 byte] By [BaltimoreJohna] at [2007-10-2 21:45:23]
# 1
Create a custom renderer. You can always look at the JTable code to get an idea of how the NumberFormat class is used to format double numbers in the table renderer.
camickra at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...
# 2

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));

}

}

BaltimoreJohna at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...
# 3
DefaultListCellRenderer doesn't implement a setValue() method. You need to override the getListCellRendererComponent() method with your code.
camickra at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...
# 4

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());

}

}

}

BaltimoreJohna at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...
# 5

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());

}

}

}

BaltimoreJohna at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...
# 6

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.

camickra at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...
# 7

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;

}

}

camickra at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...
# 8
Got it. Thanks.
BaltimoreJohna at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...
# 9

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;

}

}

BaltimoreJohna at 2007-7-14 1:00:56 > top of Java-index,Desktop,Core GUI APIs...