I have a JTable which contains a one column, cell render of each table cell is dynamically generated JPanel therefore each table cell height should be different according to its JPanel, I have tried to change the row height at the place where the jpanel is rendered but it is not working. please tell me how to set different JTable row heights in a JTable,
this is how I tried to do this,
private QCellPanel renderer = new QCellPanel();
private QCellPanel editor = new QCellPanel();
@Override
public Object getCellEditorValue() {
return editor.getQuestion();
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
renderer.setQuestion((Qusetion) value);
table.setRowHeight(renderer.getPreferredSize().height);
return renderer;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
editor.setQuestion((Qusetion) value);
table.setRowHeight(editor.getPreferredSize().height);
return editor;
}
You need to use the two-argument version
setRowHeight(rowNumber, height).If possible, you should also avoid changing the row height in the
getXComponentmethods, assetRowHeightforces the table to be redrawn, which will cause it to ask for a renderer component again, etc… The default implementation is probably smart enough to not trigger this as long as the height is stable, but if you end up with infinite loops this is were I’d look first 🙂