Overriding JTable DefaultTableCellRenderer to center all cells in JTable

I have a problem that I cannot get rid of.

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

So I have a JTable using AbstractTableModel which overrides

    public Class<?> getColumnClass(int columnIndex_p) 

      

to specify the type of each column to be displayed. One of them is boolean.

When I create a simple JTable using

    table_l = new JTable(new MyTableModel());

      

everything is ok and boolean values ​​are displayed correctly with checkboxes (on / off).

Now I would like to focus the text on each cell (and maybe 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 set this new CellRenderer for each JTable column:

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

      

But, with the new CellRenderer, the rendered JTable no longer uses the getColumnClass () method of my TableModel and thus just renders the "true / false" String in boolean values.

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

If anyone has an answer ... Thank you

EDIT: Thanks for all the clarifications you made. Actually my real question is "how to influence all DefaultRenderer JTables to make them center their result in JTable cells"

+3


source to share


2 answers


To apply the same visual appearance to all rendering components (if you want to, be careful, this can lead to a usability penalty!), You can override the JTable's prepareRenderer method:

@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
    Component comp = super.prepareRenderer(...);
    if (comp instanceof JLabel) {
        ((JLabel) comp).setHorizontalAlignment(...);
    }
    return comp;
}

      



BTW: This approach breaks the rule to not subclass JSomething for applications. You can use SwingX , which formally supports the visual appearance of the render components. Therefore, instead of subclassing, you must register the marker with the table:

JXTable table = ...
table.addHighlighter(new AlignmentHighlighter(CENTER), HighlightPredicate.ALWAYS);   

      

+2


source


The default cell renderer already does this for type values Boolean.class

, as shown here . If that's not enough, edit your question to include sscce , which has the issues you are having.

Adding. If you need further customization DefaultTableCellRenderer

, specify the renderer of the applicable type using setDefaultRenderer()

as shown here .



table.setDefaultRenderer(Boolean.class, yourCellRenderer); 

      

image

+5


source







All Articles