首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jtable自定义模型

jtable自定义模型
EN

Stack Overflow用户
提问于 2012-07-09 20:21:07
回答 2查看 2.5K关注 0票数 3

我正在与一个jTable的实现作斗争。我创建了自己的TableModel类。问题就出在这里。不知何故,我的tableData数组(Obejct[]的ArrayList)写得不正确。最后,我得到了一个表,其中所有的行都有值。

有人知道为什么ArrayList写得不正确了吗?

代码语言:javascript
复制
class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"Auftragsnummer",
                                    "Kunde",
                                    "Kunden Nr.",
                                    "Erfasst",
                                    "Kommt",
                                    "Geht",
                                    "Kommentar"};

    String[] temp_delete = new String[10];
    int index_delete = 0;

    private ArrayList<Object[]> tableData = new ArrayList<Object[]>();

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return tableData.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        Object [] temp = tableData.get(row);

        return temp[col];
    }

    public void removeAllEntry(){
        for (int i = 0; i < tableData.size(); i++) {
            tableData.remove(i);
        }
        model.fireTableDataChanged();
    }

     public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }


    public boolean isCellEditable(int row, int col) {

        switch (col){
            case 4:
                return true;
            default: return false;
        }
    }

     public void addText(Object[] object) {
        tableData.add(object);

       fireTableDataChanged();
      }

}

EN

回答 2

Stack Overflow用户

发布于 2012-07-10 01:08:33

setValueAt()的父实现什么也不做。您需要实现setValueAt()来更新您的内部数据结构tableData,并触发相应的TableModelEvent,这将更新您的视图。

代码语言:javascript
复制
    @Override
    public void setValueAt(Object aValue, int row, int col) {
        ... // update tableData
        this.fireTableCellUpdated(row, col); // notify the view
    }

顺便说一句,考虑用List<List<Object>>代替ArrayList<Object[]>

票数 2
EN

Stack Overflow用户

发布于 2012-07-10 21:13:46

多亏了垃圾神,我弄明白了(见他的帖子)。工作代码如下:

代码语言:javascript
复制
class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"Auftragsnummer",
                                    "Kunde",
                                    "Kunden Nr.",
                                    "Erfasst",
                                    "Kommt",
                                    "Geht",
                                    "Kommentar",
                                    "Abteilung"};

    String[] temp_delete = new String[10];
    int index_delete = 0;

   private ArrayList<ArrayList<Object>> tableData = new ArrayList<ArrayList<Object>>();


    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return tableData.size();

    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {

        if(tableData.size()> 0){
            return tableData.get(row).get(col);
        }

         return null;
    }

    public void removeAllEntry(){

            tableData.clear();
        model.fireTableDataChanged();
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        if(tableData.size()> 0){
            return getValueAt(0, c).getClass();
        }

        return String.class;
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.

        switch (col){
            case 4:
                return true;
            default: return false;
        }
    }


    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {

        if(tableData.size() <= row){

            ArrayList<Object> arrayList = new ArrayList<Object>();

            for(int i = 0; i < columnNames.length;i++){
                arrayList.add("");
            }

            tableData.add(arrayList);
        }

        ArrayList<Object> object = tableData.get(row);

        object.add(col, value);

        tableData.set(row, object);

        fireTableDataChanged();

    }

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11394916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档