我有一个表模型,我正在尝试实现 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;
}
}