gpt4 book ai didi

java - 如何在 TableModel 中定义主动更新的 CellEditor

转载 作者:行者123 更新时间:2023-12-01 18:51:08 25 4
gpt4 key购买 nike

我正在创建一个扩展 AbstractTableModelTableModel。但其内容会定期更改,并且列数会根据显示的数据而变化。有时,我需要使用 ComboBox 来编辑第 2 列中的单元格,有时我需要使用 ComboBox 来编辑第 3 列中的单元格。

我知道您可以通过执行 table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox));

我可以动态设置 TableModel 中的 CellEditor 吗,以便在调用 table.updateUI( 时更新 CellEditor )

编辑:我基本上是在传递我正在使用的 TableModel 一个大的 WellCollection 数据结构。冒着让您厌倦细节的风险,它由 Wells 组成,其中由 Attributes 组成,后者又由 ProposeValues 组成。不过,这并不是太重要。重要的是,我希望用户能够翻转属性,并在单击“下一步”时在表中显示不同的 ProposeValues(我之前使用 updateUI() 完成了这一点) )。

请注意,属性可以是高程类型,也可以不是高程类型。如果是高程类型,则需要额外增加一列。

这是我的代码:

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);
}
}

最佳答案

Can I set the CellEditor within my TableModel dynamically, so that the CellEditor is updated when I call table.updateUI()?

  • 不,这不是更新 JTables View 的正确方法

I'm creating a TableModel which extends AbstractTableModel. Its contents, though, changes regularly and the number of columns changes depending on the data displayed. Occasionally, I'll need to use a ComboBox to edit the cells in column 2, and occasionally I'll need a ComboBox for cells in column 3.

Can I set the CellEditor within my TableModel dynamically

  • 注意 XxxTableModel 仅将 JComboBox 的字符串值存储为 XxxTableCellEditor
<小时/>

为了获得更好的帮助,请尽快发布 SSCCE ,简短,可运行,可编译,大约是 JFrameJScrollPane 中的 JTableAbstractTableModel 的硬编码值存储为 局部变量

关于java - 如何在 TableModel 中定义主动更新的 CellEditor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15884654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com