gpt4 book ai didi

java - AbstractTableModel 使用 TreeMap 作为支持如何实现 insertRow()

转载 作者:行者123 更新时间:2023-12-01 15:48:05 24 4
gpt4 key购买 nike

我有一个表模型,我正在尝试实现 insertRow 功能。最终,我希望能够从顶部向表中添加行,这样您就不需要每次想要查看新添加的行时都向下滚动。我很难弄清楚如何实现这一点。我认为 map 不会非常有效,因为每次我在位置 0 添加新行时,其他所有内容都需要增加 1。因此,每次添加行时,我都需要将所有内容都向下推 1 。似乎一旦 table 变大,其成本就会非常高。当向底部添加值时,您只需更新该行即可。所以,我的问题是为 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;
}

}

最佳答案

不要为了解决 View 问题而不必要地使模型复杂化。相反,使用 scrollRectToVisible()正如所讨论的here 。如果以后有需要,可以使用 AbstractTableModel示例here 。该模型包含List<Value> 。实现是ArrayList<Value> ,但是不同的List实现,如​​LinkedList ,可以根据需要进行替换。

关于java - AbstractTableModel 使用 TreeMap<Integer,Vector<Object> 作为支持如何实现 insertRow(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6719196/

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