devxlogo

Display Integers Without Commas in JTables

Display Integers Without Commas in JTables

If you have a JTable where the values displayed in a particular columnare Integer objects, it will display the Integer values with commas(e.g. 98765432 is displayed as 98,765,432) if these two conditions aretrue:

1. The getColumnClass(int columnIndex) method of the JTable’sTableModel returns Integer.class for the column in which the Integerobjects are displayed.

2. The JTable is using an instance ofjavax.swing.table.DefaultTableCellRenderer to display the Integervalues.

Under normal circumstances, the second condition is usually true. Asits name implies, DefaultTableCellRenderer is the default renderer aJTable uses to display values if another rendederer is not explicitlyspecified.

The first condition may or may not be true, depending on theTableModel used by the JTable. If an instance of DefaultTableModel isbeing used, its getColumnClass() method will always returnObject.class, and the DefaultTableCellRenderer, having no way ofknowing that the Objects it is displaying are actually numeric values,will simply display those objects by calling their toString() method.For Integer objects, toString() returns a String representation of theInteger’s value that does not include commas.

Sometimes, the desired behavior might be for the TableModel’sgetColumnClass() method to return Integer.class?this is important if the table is to be sorted on an Integer column?but the JTable should display Integers without commas. The way to do this is to tell the JTable to use something other than DefaultTableCellRenderer for displaying Integers.

Writing a custom implementation of the TableCellRenderer interface isnot trivial, however. The easy solution is to create a subclass ofDefaultTableCellRenderer that displays Integers without commas, andlets its parent class handle details like cell background colors,borders, etc. This small class fills the bill:

 import javax.swing.table.*;import javax.swing.*;public class NoCommaIntegerRenderer extends DefaultTableCellRenderer {    public NoCommaIntegerRenderer() {        // make sure numbers are displayed right-justified        setHorizontalAlignment(JLabel.RIGHT);    }     // this overrides setValue() in the parent class.    public void setValue(Object value) {        if(value == null) setText("");        else setText(value.toString());    }}A JTable named "jtable" can use this class to display Integers byadding the following line of code:    jtable.setDefaultRenderer(Integer.class,                              new NoCommaIntegerRenderer());

By using this solution, a sortable JTable can still sort properly onan Integer column?100 will appear after 20 in an ascendingsort, instead of before, which is the case if the values are treatedas Objects or Strings?while at the same time, the undesiredbehavior?the formatting of Integer values with commas?is eliminated.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist