Question

I dont know why column names are named "A","B","C" ... after refreshing JTable.

I created a class to refresh JTable.

public class TableModelClass extends AbstractTableModel
{
    Object[][] data;
    Object[] title;

public TableModelClass(Object[][] dat, Object[] tit)
{
    data = dat;
    title = tit;    
}

@Override
public int getColumnCount()
{
    if(title != null)
        return title.length;
    return 0;
}

@Override
public int getRowCount()
{
    if(data != null)
        return data.length;
    return 0;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex)
{
    return data[rowIndex][columnIndex];
}

}

I set JTables values default in my class:

Object[] titlesDefault = { "tit1", "tit12","tit3"};
Object[][] dataDefault = {{ "1", "2","3"},
            {"1", "2","3"}};


       _jTable = new JTable(dataDefault, titlesDefault);    

_bAddTable.addActionListener(new ActionListener()
    {           
        @Override
        public void actionPerformed(ActionEvent arg0)
        {
                try
                {
                Object[] titles2 = { "1", "2","3", "1", "2","3"};
                Object[][] data = {{ "1", "2","3"},
                                   { "1", "2","3"},
                       {"1", "2","3"}};
                data.setModel(new TableModelClass(data, titles2));
                } 
                catch (ClassNotFoundException)
                {

                    e1.printStackTrace();
                }               

         };
        });

And after this I receive letters in column names. Why?

Was it helpful?

Solution

Without overriding getColumnName, the TableModel uses default values of "A", "B", "C", etc.

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

For this to work you need to return a String so that it corresponds to the return type in the super class. The title variable should be defined as a String[] type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top