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 displaye
The way to do this is by implementing the TableModel
interface for the student register (aka SortedMap). The TableModel
is the table model representation of how the Swing JTable expects its data. The TableModel
interface, gives full flexibilty of providing that data.
Once that implementation is created, creating a JTable
is straight on:
// As StudentRegistration class
new JTable(new StudentTableModel(studentRegistration));
// Or as SortedMap
new JTable(new StudentTableModel(students));
In this scenario I expect that the plain SortedMap
is not directly given, but a instance of StudentRegistration
, which contains a SortedMap
like that.
/**
* Models the {@link Student} entries as a Swing TableModel.
*/
final public class StudentTableModel implements TableModel {
/** The TableModel column names. */
public final String columnNames[] =
new String[] {
"Name", "Identification", "Age"
};
/** The list of TableModelListeners. */
private final ArrayList tableModelListenerList = new ArrayList();
/** The manager containing all the Student instances. */
private final StudentRegistration register;
public StudentTableModel(StudentRegistration register) {
super();
this.register = register;
}
public Class> getColumnClass(int columnIndex) {
return null;
}
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int columnIndex) {
return (columnIndex < columnNames.length) ? columnNames[columnIndex] : null;
}
public int getRowCount() {
return register.getStudents().size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
// One solution
String identification = register.getStudentIds().toArray()[rowIndex];
Student student = register.getStudent(identification);
// Other option
SortedMap studentMap = register.getStudents();
String[] studentIdArray = studentMap.keySet().toArray(new String[studentMap.keySet().size()]);
Student student = studentMap.get(studentIdArray[rowIndex]);
final Object result;
switch (columnIndex) {
case 0:
result = student.getName();
break;
case 1:
result = student.getIdentification();
break;
case 2:
result = student.getAge();
break;
default:
result = null;
}
return result;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public void setValueAt(Object value, int rowIndex, int columnIndex) {
// Just ignore, model is read only.
}
public void addTableModelListener(TableModelListener tml) {
if (! tableModelListenerList.contains(tml)) {
tableModelListenerList.add(tml);
}
}
public void removeTableModelListener(TableModelListener tml) {
tableModelListenerList.remove(tml);
}
}
PS: This sample comes partly from some other implementation, I just updated it to your scenario as described above. So it is quite possible that it contains some code glitzes. It is just provided to give you an idea on how a solution could look.