javaswingtablecellrendererabstracttablemodel

Set alignment for all columns in custom AbstractTableModel


I have a custom AbstractTableModel and I would like to center all the columns by default. I know this is made using TableCellRenderer but I don't know how to implement it in my code. Here's my class.

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;

public class BiseccionModel extends AbstractTableModel {

    private String[] columnNames = {
            "i",
            "a",
            "b",
            "xi",
            "error",
            "f(a)",
            "f(xi)"
    };

    private ArrayList<Biseccion> values;

    public BiseccionModel() {
        values = new ArrayList<Biseccion>();
    }

    public BiseccionModel(ArrayList<Biseccion> values) {
        this.values = values;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getRowCount() {
        return values.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Biseccion biseccion = getBiseccion(rowIndex);

        switch (columnIndex) {
            case 0 -> {
                return biseccion.getI();
            }
            case 1 -> {
                return biseccion.getA();
            }
            case 2 -> {
                return biseccion.getB();
            }
            case 3 -> {
                return biseccion.getXi();
            }
            case 4 -> {
                return biseccion.getError();
            }
            case 5 -> {
                return biseccion.getFa();
            }
            case 6 -> {
                return biseccion.getFxi();
            }
            default -> {
                return null;
            }
        }
    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        Biseccion biseccion = getBiseccion(rowIndex);

        switch (columnIndex) {
            case 0 -> biseccion.setI((Integer) value);
            case 1 -> biseccion.setA((Double) value);
            case 2 -> biseccion.setB((Double) value);
            case 3 -> biseccion.setXi((Double) value);
            case 4 -> biseccion.setError((Double) value);
            case 5 -> biseccion.setFa((Double) value);
            case 6 -> biseccion.setFxi((Double) value);
        }

        fireTableCellUpdated(rowIndex, columnIndex);

    }

    public Biseccion getBiseccion(int row) {
        return values.get(row);
    }
}

Is this possible / a good idea or should I stick to doing it in the class where I use the JTables with the custom models?


Solution

  • The TableModel doesn't do the rendering. However you do need to define the type of data for each column by implementing the getColumnClass(...) method, as was demonstrated in your last question, so the table can choose the appropriate renderer for the given class.

    The default renderer for Double and Integer will display the numbers right justified.

    If you really want them centered then you can use the JTable.getDefaultRenderer(...) method to get the default renderer. The default renderer is a JLabel so you can set its alignment:

    DefaultTableCellRenderer renderer = (DefaultTableCellRenderer)table.getDefaultRenderer(Double.class);
    renderer.setHorizontalAlignment(JLabel.CENTER);