通过 JSpinner 动态调整 JTable 的大小
Resize dynamically a JTable through a JSpinner
我正在考虑如何通过 JSpinner
.
实现可调整大小的 JTable
(我希望它是平方的)
所以我搜索并找到了添加列的技术,例如:
TableColumn toAdd = new TableColumn();
toAdd.setHeaderValue("col1");
toAdd.setIdentifier("col1");
toAdd.setPreferredWidth("col1".length());
this.addColumn(toAdd);
this.moveColumn(this.getColumnCount() - 1, 3);
this.validate();
与删除类似:
TableColumn j = this.getColumn(0);
this.removeColumn(j);
this.validate()
但是渲染器给我带来了麻烦,因为最后一列和第一列是具有特定视觉属性的索引。
我现在正在考虑销毁 JTable
(或至少模型)并在每次调整大小时创建一个新的,但似乎我错过了一些东西。有人有其他想法吗?
不要重新创建 JTable
或 TableModel
。相反,扩展 AbstractTableModel
并更新模型的内部数据结构。调用 fireTableStructureChanged()
会让监听 table 知道自己渲染。在微调器的 ChangeListener
.
中调用 resize()
public void resize(int rows, int cols) {
init(rows, cols);
this.fireTableStructureChanged();
}
模型不需要是正方形的;例如,n
by n+1
模型可以让您特殊处理最后一列,例如在 system of linear equations. If your model contains instances of Double
, for example, add your renderer by class
中而不是按列更改颜色最后一列。
我正在考虑如何通过 JSpinner
.
JTable
(我希望它是平方的)
所以我搜索并找到了添加列的技术,例如:
TableColumn toAdd = new TableColumn();
toAdd.setHeaderValue("col1");
toAdd.setIdentifier("col1");
toAdd.setPreferredWidth("col1".length());
this.addColumn(toAdd);
this.moveColumn(this.getColumnCount() - 1, 3);
this.validate();
与删除类似:
TableColumn j = this.getColumn(0);
this.removeColumn(j);
this.validate()
但是渲染器给我带来了麻烦,因为最后一列和第一列是具有特定视觉属性的索引。
我现在正在考虑销毁 JTable
(或至少模型)并在每次调整大小时创建一个新的,但似乎我错过了一些东西。有人有其他想法吗?
不要重新创建 JTable
或 TableModel
。相反,扩展 AbstractTableModel
并更新模型的内部数据结构。调用 fireTableStructureChanged()
会让监听 table 知道自己渲染。在微调器的 ChangeListener
.
resize()
public void resize(int rows, int cols) {
init(rows, cols);
this.fireTableStructureChanged();
}
模型不需要是正方形的;例如,n
by n+1
模型可以让您特殊处理最后一列,例如在 system of linear equations. If your model contains instances of Double
, for example, add your renderer by class
中而不是按列更改颜色最后一列。