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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing