可以使用一个键和多个值(如
)实现一个hashmapMap<Object,ArrayList<Object>> multiMap = new HashMap<Object,ArrayList<Object>>();
在Jtable?我有这样的哈希图,但我只能填2行...
答案 0 :(得分:1)
答案 1 :(得分:1)
我有这样的哈希图,但我只能填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();
}
});
}
}
您应该能够修改代码以使用ArrayLists的HashMap作为模型数据。
答案 2 :(得分:0)
只能使用Object
映射密钥。但您始终可以将Object
添加到值{/ 1}}中作为值
ArrayList