AbstractTableModel使用TreeMap <integer,vector <object>作为后台如何实现insertRow()</integer,vector <object>

时间:2011-07-16 18:01:10

标签: java swing jtable abstracttablemodel

我有一个表模型,我正在尝试实现insertRow功能。最后,我希望能够从顶部向表中添加行,这样您就不需要每次想要查看新添加的行时向下滚动。我正在努力弄清楚如何实现这一点。我认为地图不会非常有效,因为每次我在位置0添加一个新行时,其他所有内容都需要增加1.因此,每次我添加一行时我需要将所有内容向下推一个。这似乎就像一张桌子变大,它将变得非常昂贵。向底部添加值时,只需更新该行即可。那么,我的问题是为AbstractTableModel实现这个的最有效方法是什么?

public class MapTabelModel extends AbstractTableModel {

    private static Logger                logger           = OpsToolsLogger.getLogger(MapTabelModel.class.getName());

    private static final long            serialVersionUID = 4086741767870369912L;

    protected String[]                     columnNames      = null;

    protected Map<Integer, Vector<Object>> map              = null;

    /**
     * Creates a new instance of MapTableModel.
     */
    public MapTabelModel(String[] columnNames) {
        this.columnNames = columnNames;
        map = new TreeMap<Integer, Vector<Object>>();
    }

    /**
     * Add a row to the table.
     * 
     * @param row
     *            index to save the data to.
     * @param entry
     *            the data
     */
    public void addRow(int index, Vector<Object> entry) {
        if(entry != null)
        {
            try
            {
                map.put(index, entry);
            }
            catch(NullPointerException e)
            {
                //System.out.println("test");
            }
        }
    }

    /**
     * Clear the table model data.
     */
    public void clear() {
        map.clear();
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.table.TableModel#getColumnCount()
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.table.AbstractTableModel#getColumnName(int)
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    /**
     * Get entry at the given key
     * 
     * @param key
     * @return entry
     */
    public Object getEntry(int key) {
        return map.get(key);
    }

    /**
     * Get entry at the given row index
     * 
     * @param row
     * @return entry
     */
    public Object getRow(int row) {
        Object[] keys = map.keySet().toArray();
        Integer key = (Integer) keys[row];
        return map.get(key);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.table.TableModel#getRowCount()
     */
    public int getRowCount() {
        //System.out.println("maptable get size: " + map.keySet().size());
        return map.keySet().size();
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    public Object getValueAt(int row, int column) {
        Object obj = null;
        try {

            Object[] keys = map.keySet().toArray();//this create out of memomry errors???

            if (row < keys.length) {
                Integer key = (Integer) keys[row];
                Vector<Object> vector = map.get(key);
                if(vector != null && column < vector.size())
                {

                    obj = vector.get(column);
                }
                else
                {
                    //System.out.println("Could not get value at row: " + row + " column: "+ column);
                }
            }
        } catch (ConcurrentModificationException e) {
            logger.info(e.toString());
        }

      //  Vector v = map.get(new Integer(row));
      //  Object obgj2 = v.get(column);
        return obj;
    }

    /**
     * Get the entry at the given key and column.
     * 
     * @param key
     * @param column
     * @return entry
     */
    public Object getValueAtByKey(int key, int column) {
        return map.get(key).get(column);
    }

    /**
     * Remove the entry at the given row
     * 
     * @param row
     * @return the entry removed
     */
    public Object removeRow(int row) {
        Object[] keys = map.keySet().toArray();
        Integer key = (Integer) keys[row];
        return map.remove(key);
    }

    /**
     * Remove the entry by the key
     * 
     * @param key
     * @return the entry removed
     */
    public Object removeRowByKey(int key) {
        return map.remove(key);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object,
     * int, int)
     */
    @Override
    public void setValueAt(Object value, int row, int column) {
        Object[] keys = map.keySet().toArray();
        Integer key = (Integer) keys[row];
        map.get(key).set(column, value);
    }

    /**
     * Set the value at the given key and column
     * 
     * @param value
     * @param key
     * @param column
     */
    public void setValueByKey(Object value, int key, int column) {
        if( map.get(key) != null)
        {
            map.get(key).set(column, value);
        }
    }

    public void setColumnNames(String[] columnNames) {
        this.columnNames = columnNames;
    }

}

1 个答案:

答案 0 :(得分:1)

不要不必要地使模型复杂化以解决视图问题。相反,使用 正如所讨论的scrollRectToVisible() here。如果以后需要,则会有一个AbstractTableModel示例here。该模型包含List<Value>。实施是ArrayList<Value>,但可以根据需要替换List不同的LinkedList实施,例如{{1}}。