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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.