它可以實現一個HashMap一個鍵和多個值一樣在Jtable中有一個鍵和多個值的hashmap?
Map<Object,ArrayList<Object>> multiMap = new HashMap<Object,ArrayList<Object>>();
JTable中
?我有這樣一個HashMap,但是我只是能夠填補2行...
它可以實現一個HashMap一個鍵和多個值一樣在Jtable中有一個鍵和多個值的hashmap?
Map<Object,ArrayList<Object>> multiMap = new HashMap<Object,ArrayList<Object>>();
JTable中
?我有這樣一個HashMap,但是我只是能夠填補2行...
我有這樣一個HashMap,但我只是能夠填補2行...
您的TableModel有問題。
下面是用一個例子:
Map<Integer, Map<Integer, Object>> data = new HashMap<Integer, HashMap<Integer, Object>>();
的數據存儲爲TableModel:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableSparse extends JPanel
{
public TableSparse(int row, int column)
{
setLayout(new BorderLayout());
TableModel model = new SparseTableModel(row, column);
JTable table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
model.setValueAt("one", 0, 0);
model.setValueAt("two", 5, 5);
}
class SparseTableModel extends AbstractTableModel
{
Map<Integer, Map<Integer, Object>> data = new HashMap<Integer, Map<Integer, Object>>();
int rows;
int columns;
public SparseTableModel(int rows, int columns)
{
this.rows = rows;
this.columns = columns;
}
@Override
public int getRowCount() { return rows; }
@Override
public int getColumnCount() { return columns; }
@Override
public boolean isCellEditable(int row, int column) { return true; }
@Override
public Object getValueAt(int row, int column)
{
// Check for row
Integer key = new Integer(row);
Map<Integer, Object> rows = data.get(key);
if (rows == null) return null;
// Now check for column
key = new Integer(column);
return rows.get(key);
}
@Override
public void setValueAt(Object value, int row, int column)
{
// Remove cell data
if (value.toString().equals(""))
{
removeCellData(row, column);
return;
}
// Save cell data
Integer key = new Integer(row);
Map<Integer, Object> rows = data.get(key);
if (rows == null)
{
rows = new HashMap<Integer, Object>();
data.put(key, rows);
}
key = new Integer(column);
rows.put(key, value);
}
private void removeCellData(int row, int column)
{
Integer rowKey = new Integer(row);
Map<Integer, Object> rows = data.get(rowKey);
if (rows == null) return;
Integer columnKey = new Integer(column);
rows.remove(columnKey);
if (rows.isEmpty())
data.remove(rowKey);
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableSparse(100, 256));
frame.setSize(600, 600);
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
您應該可以修改代碼以使用的ArrayList的一個HashMap的模型數據。
很好的例子; [編碼到接口] +1(http://stackoverflow.com/q/383947/230513);也考慮'Map
@trashgod,這是我曾經躺過的舊代碼(從我第一次瞭解自定義TableModels時開始),而且實際上我嘗試更新代碼,以便早期使用帶有Map
非常感謝你......這可以幫助我很多! – okaisan1 2014-09-15 06:39:22
的關鍵可以用Object
映射而已。不過你可以隨時添加一個Object
到ArrayList
存在價值
Map<Object,ArrayList<Object>> multiMap = new HashMap<Object,ArrayList<Object>>();
Object key ;//key to be searched
Object value ;
if(multiMap.get(key)==null){
ArrayList<Object> arObj = new ArrayList<Object>();
arObj.add(value);
multiMap.put(key,arObj);
}
else{
multiMap.get(key).add(value);
}
你是什麼意思「只能夠填寫2行...」? – RyPope 2014-09-12 13:03:06