What you’re looking for is a Row header (line header - compare with column header, used when you want a line fixed at the top). There is a similar question in Stackoverflow in English, but the answers do not contain much information, only two links.
I took a quick look at both of them, and they separate the JTable
in two... However, the first link allows using a single TableModel
for both tables - which is what really matters, right?
See the link to the complete solution. Example of use (simplified):
TableModel data = new DefaultTableModel(0,10); // Substitua com seu TableModel particular
// Criando o modelo de coluna para a tabela "normal" (coluna 1 em diante)
TableColumnModel columns = new DefaultTableColumnModel();
for (int count = data.getColumnCount(), i = 1; i < count; i++)
{
TableColumn c = new TableColumn(i);
c.setHeaderValue(data.getColumnName(i));
columns.addColumn(c);
}
// Criando o modelo de coluna para o cabeçalho (coluna 0; se quiser pode fixar mais colunas)
TableColumnModel headerColumns = new DefaultTableColumnModel();
TableColumn h = new TableColumn(0);
h.setHeaderValue(data.getColumnName(0));
headerColumns.add(h);
// Criando as tabelas
JTable table = new JTable(data, columns);
JTable rowHeader = new JTable(data, headerColumns);
// RowHeaderRendered é uma classe externa, que pode ser baixada no link acima
rowHeader.setDefaultRenderer(Object.class, new RowHeaderRenderer());
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setRowHeaderView(rowHeader);
I don’t know if this is really possible. It seems to me that your solution of splitting the table in two is the best.
– Victor Stafusa
@Victor Apparently you can’t escape from raising two
JTable
, but it seems possible to use a singleTableModel
and make each table (header and "rest") use only one part of that template.– mgibsonbr