How to access columns by column index?

    model = new DefaultTableModel(data, columnNames) 
    {            
        @Override
        public String getColumnName(int col) {
            return (String) columnNames[col];
        }
    };

   table = new JTable(model); 

   for (int tc=0; tc<table.getColumnCount(); tc++)
        table.getColumn(tc).setCellRenderer(new TextAreaRenderer());

I need to access columns by column index. The following error has occurred:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Identifier not found
at javax.swing.table.DefaultTableColumnModel.getColumnIndex(Unknown Source)
    at javax.swing.JTable.getColumn(Unknown Source)
+4
source share
2 answers

You need to use the column name to get the column as shown below:

 for (int tc=0; tc<table.getColumnCount(); tc++)
        table.getColumn(columnNames[tc]).setCellRenderer(new TextAreaRenderer());
+1
source

TableColumnModel has a getColumn (int columnIndex) method:

TableColumnModel columnModel = table.getColumnModel();
for (int tc = 0; tc < table.getColumnCount(); tc++)
    columnModel.getColumn(tc).setCellRenderer(new TextAreaRenderer());
0
source

Source: https://habr.com/ru/post/1530972/


All Articles