gpt4 book ai didi

java - 在 JTable 中显示哈希表数据?

转载 作者:行者123 更新时间:2023-12-01 13:13:08 25 4
gpt4 key购买 nike

我有一个哈希表

hashtable c = new Hashtable();

Employee emp = new Employee("E1001","Sky");
c.put("E1001",emp);

然后我就有了一个 JTable

Object[][] data = {

{"", ""},

};

String[] headers = {"Employee Code", "Employee First Name"};
JTable table = new JTable(data, headers);

我似乎无法弄清楚如何将 hashtable 项添加到 JTable

最佳答案

如果我没看错的话,就像......

Object[][] data = new Object[c.size()][2];
int row = 0;
for (Object key : c.keySet()) {
data[row][0] = key;
Employee emp = (Employee)c.get(key);
data[row][1] = ...; // Get name from Employee object...
// Personally, I prefer to assign the Employee object to
// the column of the row and use a TableCellRenderer to
// renderer it
}

String[] headers = {"Employee Code", "Employee First Name"};
JTable table = new JTable(data, headers);

应该可以...

现在,如果您希望在 HashMapTableModel 之间建立更牢固的关系(这样您就可以向表中添加内容,它会更新 HashMap),您将需要使用 AbstractTableModel 并亲手映射模型要求和 HashMap

之间的内容

关于java - 在 JTable 中显示哈希表数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22680691/

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