Java 如何在TableModel中定义主动更新CellEditor

Java 如何在TableModel中定义主动更新CellEditor,java,swing,jcombobox,abstracttablemodel,Java,Swing,Jcombobox,Abstracttablemodel,我正在创建一个TableModel,它扩展了AbstractTableModel。不过,它的内容会定期变化,列数也会根据显示的数据而变化。有时,我需要使用组合框来编辑第2列中的单元格,有时我需要使用组合框来编辑第3列中的单元格 我知道您可以通过执行table.getColumnModel().getColumn(2.setCellEditor)(新的DefaultCellEditor(myComboBox)),来设置表的默认呈现器 我是否可以在我的TableModel中动态设置CellEdito

我正在创建一个
TableModel
,它扩展了
AbstractTableModel
。不过,它的内容会定期变化,列数也会根据显示的数据而变化。有时,我需要使用组合框来编辑第2列中的单元格,有时我需要使用组合框来编辑第3列中的单元格

我知道您可以通过执行
table.getColumnModel().getColumn(2.setCellEditor)(新的DefaultCellEditor(myComboBox)),来设置表的默认呈现器

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

编辑:我基本上是在传递我正在使用的
表格模型
一个大的
井集合
数据结构。它由
组成,由
属性
组成,由
建议值
组成,可能会让您对细节感到厌烦。不过这并不太重要。重要的是,我希望用户能够在单击“下一步”(我以前使用
updateUI()
)时,在属性之间切换,并在表中显示不同的
proposedvalue

请注意,
属性可以是立面类型,也可以不是立面类型。如果是立面类型,则其中需要有一个额外的柱

这是我的密码:

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);
    }
}
我是否可以在TableModel中动态设置CellEditor,以便 调用table.updateUI()时CellEditor会更新吗


  • 不,这不是更新JTables视图的正确方法
我正在创建一个扩展AbstractTableModel的TableModel。它的 不过,内容会定期更改,列数也会更改 取决于显示的数据。有时,我需要使用 ComboBox编辑第2列中的单元格,有时我需要一个 第3列中单元格的组合框

  • 有什么问题吗
我可以在TableModel中动态设置CellEditor吗

  • 请注意,XxxTableModel仅将JComboBox的字符串值存储为XxxTableCellEditor

为了获得更好的帮助,请尽快在
JScrollPane
中发布一个简短、可运行、可编译的、带有
JTable
的JFrame
,并将
AbstractTableModel
的硬编码值存储为
局部变量

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


  • 不,这不是更新JTables视图的正确方法
我正在创建一个扩展AbstractTableModel的TableModel。它的 不过,内容会定期更改,列数也会更改 取决于显示的数据。有时,我需要使用 ComboBox编辑第2列中的单元格,有时我需要一个 第3列中单元格的组合框

  • 有什么问题吗
我可以在TableModel中动态设置CellEditor吗

  • 请注意,XxxTableModel仅将JComboBox的字符串值存储为XxxTableCellEditor

为了获得更好的帮助,请尽快在
JScrollPane
中发布一个简短、可运行、可编译的、带有
JTable
的JFrame
,并将
AbstractTableModel
的硬编码值存储为
局部变量

我可以在TableModel中动态设置CellEditor吗

CellEditor不由TableModel确定

JTable确定使用哪个编辑器

您可以重写table.getCellEditor(…)方法来动态更改正在使用的编辑器。请参阅:了解此方法的示例

我可以在TableModel中动态设置CellEditor吗

CellEditor不由TableModel确定

JTable确定使用哪个编辑器


您可以重写table.getCellEditor(…)方法来动态更改正在使用的编辑器。请参阅:有关此方法的示例。

不要调用updateUI()
。应用程序代码重新绘制表时不需要这样做。问题,是否希望将JComboBox的项更改为XxxCellEditor,或关于单元格是否可编辑,AbstractTableModel的硬编码值存储为局部变量
不调用updateUI()
。应用程序代码重新绘制表时不需要这样做。问题,您是否希望将JComboBox的项更改为XxxCellEditor,或者关于单元格是否可编辑,AbstractTableModel的硬编码值是否存储为局部变量“这不是更新JTables视图的正确方法”-请解释!另外,我粘贴了我的代码,但是考虑到我传递给TableModel的数据结构的复杂性,我不确定是否可以轻松地制作一些小的、可执行的东西粘贴到这里。我是否应该创建一个模拟WellCollection对象功能的虚拟数据结构,这样您就可以看到发生了什么?如果您想从外部动态更改JComboBox的项,或者每个JComboBox的项都可能不同(请参阅camickr在本线程中的答案),或者JComboBox被链接,这意味着从一个JComboBox选择将改变另一个JComboBox()的底层ComboxModel,JComboBox作为TableCellEditor与JTTable无关,这是单独的JCComponent用作编辑器,最后选择的值存储在XxxTableModel中“这不是更新JTTables视图的正确方法”-请解释!另外,我粘贴了我的代码,但是考虑到我传递给TableModel的数据结构的复杂性,我不确定是否可以轻松地制作一些小的、可执行的东西粘贴到这里。我是否应该创建一个模拟WellCollection对象功能的虚拟数据结构,以便您可以看到发生了什么?仍然存在问题吗