La solución de Camickr no funcionó para mí en absoluto. Sin embargo, mi modelo de datos fue dinámico: cambió todo el tiempo. Supongo que la solución mencionada funciona para datos estáticos, como si provinieran de una matriz.
Tenía JPanel para componente de procesador de celda y su tamaño preferido no se configuró correctamente después de usar prepareRenderer (...). El tamaño se configuró correctamente después de que la ventana contenedora ya estaba visible y se volvió a pintar (2 veces, de hecho, después de un tiempo no especificado, aunque corto). ¿Cómo podría llamar al método updateRowHeights() que se muestra arriba entonces y dónde haría esto? Si lo llamé (reemplazado) Table.paint() obviamente causó infinitos repintados. Me tomó 2 días. Literalmente. La solución que funciona para mí es esta (este es el renderizador de células que utilicé para mi columna):
public class GlasscubesMessagesTableCellRenderer extends MyJPanelComponent implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
//this method updates GUI components of my JPanel based on the model data
updateData(value);
//this sets the component's width to the column width (therwise my JPanel would not properly fill the width - I am not sure if you want this)
setSize(table.getColumnModel().getColumn(column).getWidth(), (int) getPreferredSize().getHeight());
//I used to have revalidate() call here, but it has proven redundant
int height = getHeight();
// the only thing that prevents infinite cell repaints is this
// condition
if (table.getRowHeight(row) != height){
table.setRowHeight(row, height);
}
return this;
}
}
¿Necesito un modelo de tabla para esto? – Wulf
¿Por qué atrapar ClassCastException? No hay lanzamientos ni operaciones sin marcar. –
@KarlRichter, buen punto, no estoy seguro de por qué está allí. El try/catch ha sido eliminado. – camickr