Automatycznie dostosuj kolumnę Jtable, aby dopasować zawartość
Próbuję dopasować szerokość kolumny JTable w zależności od danych wewnątrz. Mój kod:
for(int column = 0; column < gui.testsuiteInfoTable.getColumnCount(); column ++){
int width =0;
for (int row = 0; row < gui.testsuiteInfoTable.getRowCount(); row++) {
TableCellRenderer renderer = gui.testsuiteInfoTable.getCellRenderer(row, column);
Component comp = gui.testsuiteInfoTable.prepareRenderer(renderer, row, column);
width = Math.max (comp.getPreferredSize().width, width);
System.out.println(width);
}
TableColumn col = new TableColumn();
col = gui.testsuiteInfoTable.getColumnModel().getColumn(column);
System.out.println(width);
col.setWidth(width);
gui.testsuiteInfoTable.revalidate();
}
}
Rozmiary są poprawne, ale kolumny tabeli nadal mają tę samą szerokość! Tabela jest osadzona w ScrollPane w GridBagLayout, czy to jest problem? Dziękujemy za wszelkie sugestie.