英文:
Make JTable cells perfectly square
问题
There is a way to make a JTable's cells perfectly square. You can achieve this by overriding the getPreferredScrollableViewportSize
method in the table's implementation:
@Override
public Dimension getPreferredScrollableViewportSize() {
int tableWidth = this.getColumnModel().getTotalColumnWidth();
int tableHeight = this.getRowHeight() * this.getRowCount();
return new Dimension(tableWidth, tableHeight);
}
This will ensure that the table's cells are square.
英文:
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?
答案1
得分: 1
Sure, here's the translated content:
在我的评论中,您需要使用一些逻辑来判断宽度或高度哪个较小(限制因素)。然后根据较小的尺寸来更改单元格的大小。
与其在行高上使用覆盖并试图操纵列覆盖,我建议拦截 JTable 的 componentResized
事件,并简单地设置所需的大小。我假设此示例中有固定数量的单元格(15x15):
int rowCount = 15;
int colCount = 15;
your_JTable.addComponentListener(new ComponentAdapter(){
@Override
public void componentResized(ComponentEvent e){
//获取新的 JTable 组件大小
Dimension size = getSize();
int cellSize;
//检查高度或宽度哪个是限制因素,并相应地设置单元格大小
if (size.height / rowCount > size.width / colCount){
cellSize = size.width / colCount;
}
else{
cellSize = size.height / rowCount;
}
//将新行高设置为我们的新尺寸
setRowHeight(cellSize);
//将新列宽设置为我们的新尺寸
for (int i = 0; i < getColumnCount(); i++){
getColumnModel().getColumn(i).setMaxWidth(cellSize);
}
}
});
这在 JDK13 中提供了完美的正方形单元格,无论是水平还是垂直调整大小,或者两者兼而有之。我意识到我也回答了您的先前的问题,因此在这里是一个使用该代码的工作示例:
英文:
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 componentResized
event and simply set the sizes we want. I have assumed a fixed number of cells (15x15) for this example:
int rowCount = 15;
int colCount = 15;
your_JTable.addComponentListener(new ComponentAdapter(){
@Override
public void componentResized(ComponentEvent e){
//Get new JTable component size
Dimension size = getSize();
int cellSize;
//Check if height or width is the limiting factor and set cell size accordingly
if (size.height / rowCount > size.width / colCount){
cellSize = size.width / colCount;
}
else{
cellSize = size.height / rowCount;
}
//Set new row height to our new size
setRowHeight(cellSize);
//Set new column width to our new size
for (int i = 0; i < getColumnCount(); i++){
getColumnModel().getColumn(i).setMaxWidth(cellSize);
}
}
});
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论