如何在JTable中使用hashMap [英] how to use hashMap with JTable
问题描述
我有一个hashMap,我希望它的数据可以在JTable中查看,但是我无法获得hashMap数量的列和行以及要显示的数据。我有一个hashmap,它将accountID作为密钥和学生的对象,其中每个学生都有他们的数据,如姓名,身份证,年龄等,无论如何引用JTable文档,它说我需要int为行和列以及类型为Object的多维数组。我该怎么做?我可以将我的hashMap更改为multidimenion数组吗?
i have a hashMap which i would like its data to be viewed in a JTable how ever i am having trouble getting the hashMap amount of columns and rows and the data to be displayed.i have a hashmap which takes a accountID as the key and a object of students in which each students have their data like name,id, age, etc.however referring to the JTable docs, it says i would need ints for the row and column and a multidimension array of type Object. how can i do it? can i change my hashMap into a multidimenion array?
- 编辑我已经编辑了我的问题所以它可以更清楚,我相当新的Java我不是真的得到你们有些人发布的内容,特别是因为我正在做的工作与OO有很大关系,掌握OO概念是我最大的挑战,
--Edit i have edited my question so it could be more clear , i am fairly new to Java i do not really get what some of you have posted, especially since the work i am doing is quite related to OO and grasping OO concepts is my biggest challenge,
/ 我有一个dataStorage类,注册用户被添加到HashMap,其用户名的Key输入是getUser。 /
/I have a dataStorage class, the registered user is added to the HashMap with a Key input of his Username, which is getUser ./
import java.util.*;
public class DataStorage
{
HashMap<String, Student> students = new HashMap<String, Student>();
HashMap<String, Staff> staffMembers = new HashMap<String, Staff>();
//Default constructor
public DataStorage(){
}
public void addStaffMember(Staff aAcc)
{
staffMembers.put(aAcc.getUser(),aAcc);
}
public void addStudentMember(Student aAcc)
{
students.put(aAcc.getUser(),aAcc);
}
public Staff getStaffMember(String user)
{
return staffMembers.get(user);
}
public Student getStudent(String user)
{
return students.get(user);
}
public int getStudentRows()
{
return students.size();
}
}
/ ** **这是一个扩展帐户的学生班级*** /
/**** This is a student class which extends Account***/
public class Student extends Account {
private String studentNRIC;
private String diploma;
private String gender;
private double level;
private int credits;
private int age;
private boolean partTime;
private boolean havePc;
private boolean haveChild;
public Student(String n, String nr, String id, String dep, String user, String pass)
{
super(n, dep, user, pass, id);
studentNRIC = nr;
}
public void setPartTime(boolean state)
{
if(state == true)
{
partTime = true;
}
else
{
partTime = false;
}
}
public boolean getPartTime()
{
return partTime;
}
public void setHavePc(boolean state)
{
if(state == true)
{
havePc = true;
}
else
{
havePc = false;
}
}
public boolean getHavePc()
{
return havePc;
}
public void setHaveChild(boolean state)
{
if(state == true)
{
haveChild = true;
}
else
{
haveChild = false;
}
}
public boolean getHaveChild()
{
return haveChild;
}
public void setDiploma(String dip)
{
diploma = dip;
}
public String getDiploma()
{
return diploma;
}
public void setCredits(String cre)
{
credits = Integer.parseInt(cre);
}
public int getCredits()
{
return credits;
}
public void setGender(String g)
{
gender = g;
}
public String getGender()
{
return gender;
}
public void setAge(String a)
{
age = Integer.parseInt(a);
}
public int getAge()
{
return age;
}
public void setLevel(String lvl)
{
level = Double.parseDouble(lvl);
}
public double getLevel()
{
return level;
}
public void setStudentNRIC(String nr)
{
studentNRIC = nr;
}
public String getStudentNRIC()
{
return studentNRIC;
}
}
/ ****这是a账户超类*** /
/**** This is a the Account superclass***/
public class Account {
private String name;
private String department;
private String username;
private String password;
private String accountID;
public Account()
{
}
public Account(String nm,String dep,String user,String pass, String accID)
{
name = nm;
department = dep;
username = user;
password = pass;
accountID = accID;
}
public void setName(String nm)
{
name = nm;
}
public String getName()
{
return name;
}
public void setDep(String d)
{
department = d;
}
public String getDep()
{
return department;
}
public void setUser(String u)
{
username = u;
}
public String getUser()
{
return username;
}
public void setPass(String p)
{
password = p;
}
public String getPass()
{
return password;
}
public void setAccID(String a)
{
accountID = a;
}
public String getAccID()
{
return accountID;
}
}
推荐答案
您有几种选择给您。我可能会构建我自己的 TableModel
并将 HashMap
转换为List,但这需要 accountID
是学生的一部分,我无法判断它是否来自您的帖子。因此创建多维数组可能更容易。为此,您需要检查 HashMap
中的每个对象,为此我们将使用'循环'。
You have several options available to you here. I would probably build my own TableModel
and convert the HashMap
into a List, but that would require that accountID
was part of Student and I cannot tell if it is from your post. So probably easier to create a multi dimensional array. To do this you need to examine every object in your HashMap
and to do this we would use a 'loop'.
首先创建数组以保存数据:
First create the array to hold your data:
Object[][] tableData = new Object[students.keySet().size()][numberOfColumns];
将numberOfColumns替换为您的表格所具有的列数。
Replace numberOfColumns with the number of columns your table has.
int index = 0;
for (String key : students.keySet())
{
Student student = students.get(key);
tableData[index][0] = student.getXXX
tableData[index][1] = student.getYYY
tableData[index][2] = student.getZZZ
// and so forth
index++;
}
所以我们在这里做的是创建一个循环来检查学生HashMap并使用该键检索Student对象并使用正确的数据填充数组。
So what we do here is create a loop that will examine every key in the students HashMap and with that key we retrieve the Student object and populate the array with the correct data.
这是为了回答您的问题,但我建议您查看TableModel接口,并在学生的HashMap周围构建一个。更有男子气概:)
This is to answer your question, but I would recommend that you take a look at the TableModel interface and build one around your HashMap of Students. More manly :)
这篇关于如何在JTable中使用hashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!