我正在创建一个TableModel
which extends AbstractTableModel
。但是,它的内容会定期更改,并且列数会根据显示的数据而变化。有时,我需要使用 aComboBox
来编辑第 2 列中的单元格,有时我需要使用 a ComboBox
for 第 3 列中的单元格。
我知道您可以通过执行设置表格的默认渲染器table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox));
我可以CellEditor
在我的内部TableModel
动态设置,以便CellEditor
在我调用时更新table.updateUI()
吗?
编辑:我基本上是在传递我正在使用TableModel
的大WellCollection
数据结构。冒着让你厌烦细节的风险,它由 组成Wells
,由 组成Attributes
,由 组成ProposedValues
。不过,这不是很重要。重要的部分是我希望用户能够ProposedValues
在单击下一步时翻阅属性并在表格中具有不同的显示(我之前使用 完成updateUI()
)。
请注意,anAttribute
可以是高程类型,也可以不是高程类型。如果是高程类型,则需要在其中增加一列。
这是我的代码:
public class ProposedValueTableModel extends AbstractTableModel{
private WellCollection wells;
public ProposedValueTableModel(WellCollection wells){
this.wells = wells;
}
@Override
public int getColumnCount() {
// if we're dealing with elevation values, we want fields for the value, the elevation type,
// the source, and additional comments. If not, only show value, source, and comments.
if(wells.getCurrAttribute().isElevationType())
return 4;
else return 3;
}
@Override
public int getRowCount() {
//NOTE: add 1 for extra, blank row!
return wells.getCurrAttribute().getProposedValues().size()+1;
}
@Override
public Object getValueAt(int row, int col) {
//TODO: convert this to handy dandy switch statements
// make the last row blank to allow entries
if (row==wells.getCurrAttribute().getProposedValues().size())
return "";
ProposedValue propVal = wells.getCurrAttribute().getProposedValues().get(row);
//if we're NOT dealing with an elevation type, simply have three fields
if(wells.getCurrAttribute().isElevationType()==false){
switch(col){
case 0:
return propVal.val;
case 1:
return propVal.source;
case 2:
return propVal.comment;
}
}
// if it IS an elevation value, include elevation type
else{
switch(col){
case 0:
return propVal.val;
case 1:
return propVal.elevType;
case 2:
return propVal.source;
case 3:
return propVal.comment;
}
}
return "";
}
public String getColumnName(int col){
if(wells.getCurrAttribute().isElevationType() ==false){
switch(col){
case 0:
return "Proposed value";
case 1:
return "Source";
case 2:
return "Comment";
}
}
else{
switch(col){
case 0:
return "Proposed value";
case 1:
return "Type";
case 2:
return "Source";
case 3:
return "Comment";
}
}
return "header";
}
public boolean isCellEditable(int row, int col){
return true;
}
public void setValueAt(Object value, int row, int col){
// we're adding something to the last row, then add a new ProposedValue to the proposedValues array
if(row == wells.getCurrAttribute().getProposedValues().size()){
wells.getCurrAttribute().getProposedValues().add(new ProposedValue());
}
ProposedValue propVal = wells.getCurrAttribute().getProposedValues().get(row);
if(wells.getCurrAttribute().isElevationType()==false){
switch(col){
case 0:
// Value
propVal.val = (String)value; break;
case 1:
// Source
propVal.source = (String)value; break;
case 2:
// Comment
propVal.comment = (String)value; break;
}
}
else{
switch(col){
case 0:
// Value
propVal.val = (String)value; break;
case 1:
// Elevation type
propVal.elevType = (String)value; break;
case 2:
// Source
propVal.source = (String)value; break;
case 3:
// Comment
propVal.comment = (String)value; break;
}
}
//TODO: find out why this is necessary
fireTableCellUpdated(row, col);
}
}