Overriding JTable's DefaultTableCellRenderer to center all the cells in a JTable

adrien.pain picture adrien.pain · Feb 8, 2013 · Viewed 7.2k times · Source

I've got a problem I can't get rid of.

Just so you know, I'm fairly new to using JTables, so the answer might be simple, but I can't find a solution :/

So, I've got a JTable using an AbstractTableModel, which overrides the

    public Class<?> getColumnClass(int columnIndex_p) 

method, to tell the type of each column to be displayed. One of them is a Boolean.

When I create a simple JTable, using

    table_l = new JTable(new MyTableModel());

everything is fine, and Boolean values are correctly displayed using checkboxes (on/off).

Now, I'd like to center the text on each cell (and, possibly more options later).

So I define a new DefaultTableCellRenderer for each column, like this :

    DefaultTableCellRenderer cellRenderer_l = new DefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
          // delegate the rendering part to the default renderer (am i right ???)
          Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
          return comp;
        }
    }

and then I just set the horizontal alignment of this CellRender with :

    cellRenderer_l.setHorizontalAlignment(JLabel.CENTER);

Then I install this new CellRenderer on each column of the JTable :

    for (int i = 0; i < table_l.getColumnCount(); ++i) {
        table_l.getColumnModel().getColumn(i).setCellRenderer(cellRenderer_l);
    }

But, with the new CellRenderer, the displayed JTable isn't using the getColumnClass() method of my TableModel anymore, and thus just display "true/false" String on Boolean values.

I don't know how to get it to still use the getColumnClass() as before.

If someone has the answer... Thank you

EDIT: thanks for all the clarifications you made. In fact, my real question was : "how to affect all DefaultRenderer of a JTable to make them center their result in the JTable's cells"

Answer

trashgod picture trashgod · Feb 8, 2013

The default cell renderer already does this for values of type Boolean.class, as shown here. If this is not sufficient, please edit your question to include an sscce that exhibits any problems you encounter.

Addendum: If you need to further customize a DefaultTableCellRenderer, specify the renderer for the applicable type using setDefaultRenderer(), as shown here.

table.setDefaultRenderer(Boolean.class, yourCellRenderer); 

image