Stack Exchange
Swing - Change table model reset the renderer. Why?
My SSCCE:
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Renderer_Model_LookAndFeel extends JFrame {
    public Renderer_Model_LookAndFeel() {
        begin();
    }

    private void begin() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        Object[][] data = new Object[4][4];
        Object[] title = new Object[] {"Name", "Gender", "Age", "Prof."};

        JTable table = new JTable(new DefaultTableModel(data, title));

        for (int i=0; i<4; i++) {
            for (int j=0; j<4; j++) {
                data[i][j] = "Tom"; 
            }
        }


        //SET RENDERER
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(SwingConstants.RIGHT);
        table.getColumnModel().getColumn(2).setCellRenderer(r);

        table.setModel(new DefaultTableModel(data, title));

        table.setPreferredScrollableViewportSize(new Dimension(500, 300));

        JScrollPane sp = new JScrollPane(table);
        sp.setBounds(0, 0, 600, 400);
        getContentPane().add(sp);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Renderer_Model_LookAndFeel frame = new Renderer_Model_LookAndFeel();

            }

        });
    }
}

The key is in the order of these lines:
table.setModel(new DefaultTableModel(data, title));

And:
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(SwingConstants.RIGHT);
table.getColumnModel().getColumn(2).setCellRenderer(r);

If I first set the model and then change the column renderer, the third column will be right-aligned. But, if I set the renderer first and then change model, the renderer will be reset to null and then, respects the default alignment.
Why? 
In my project we first establish the UI part in method A, and then read a XML file to fill the data in method B, in order to separate the data with the UI presentation. So, I do it in this order:

Construct all the UI components, including the table, fill it with an empty data vector;
Read the XML to get the data.
Reset the table data.

So the problem occurs. I have no problem switching the order (although it mixes the data part with UI), but I just want to know the reason. 

why would you set a new DefaultTableModel 2 times? i would guess because you override the given model , which the renderer worked with and then it becomes null. set the model at initialization of the table and never again after

This is actually what setModel does

Well it is what should be like I guess. I added more details.

create a DefaultTableModel and work with it all the time and add data to the model with model.addRow()

@XtremeBaumer ye it's the way it should be.. and more clear because yes, we work with rows.

And @MadProgrammer, so some details?

@FaithReaper setting of the new table model causes recreation of the column model. You can turn it off using the method JTable.setAutoCreateColumnsFromModel(boolean), but in this case you need to install the column model by yourself.

JTabel#setModel calls tableChanged, this in turn, but default, calls createDefaultColumnsFromModel, this is, in part, what's changing the cell renderer, as it creates all new TableColumns - solution, don't set the renderer against the column, but set it against the column type by calling JTable#setDefaultRenderer, passing in the Class type of the column which is returned by TableModel#getColumnClass. Personally, I'd avoid setAutoCreateColumnsFromModel because it introduces a considerable amount of overhead and could cause a lot of problems if it gets out of sync, but that's me
