Is there any mechanism to make a JTable's cells perfectly square? Currently I'm just overriding this method in the table's implementation:
@Override
public int getRowHeight() {
return this.getColumnModel().getColumn(0).getWidth();
}
This works in most cases like in the image below where I have a 15x15 JTable:
But if I extend the JPanel the table sits on width wise the cell's continue to expand width and length wise resulting in the lower 3 rows being cut off:
I am wondering if there is a better solution to have a JTable's cells be perfectly square?


Further on my comment, you would need to use some logic to work out if the width or height is smaller (The limiting factor). Then based on which is smaller you can change the size of the cells.
Rather than using an override on the row height and trying to mess with column overrides, I instead reccomend intercepting the JTable
componentResizedevent and simply set the sizes we want. I have assumed a fixed number of cells (15x15) for this example:This provides perfectly square cells in JDK13 when resized both horizontally or vertically or both. I realise that I answered your previous question as well, so here is a working example using that code: