This is a short tutorial on how to use Table models.
Simple JTable that contains person data
The first step is to create the Table Model, this can be done implementing the TableModel interface or extending AbstractTableModel. AbstractTableModel has already implemented some of the basic functionality i.e. event handling, therefore we will use this as the basis for our model. AbstractTableModel contains abstract methods that needs to be implemented in order for the JTable to work correctly - the methods annoted with @Override
public class Person{
private String firstName, lastName;
public Person(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
//getters
}
public class PersonTableModel extends AbstractTableModel {
private static String[] COLUMN_NAMES = {"First name", "Last name"};
private List<Person> persons;
public PersonTableModel(List<Person> persons){
this.persons = persons;
}
@Override
public int getRowCount() {
return persons.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Person person = persons.get(rowIndex);
String rowText = "";
switch (columnIndex) {
case 0: rowText = person.getFirstName(); break;
case 1: rowText = person.getlastName(); break;
}
return rowText;
}
}


1 comment:
Mockito is one of the related concepts of RoboGuice and Robolectric. I need the RoboGuice Tutorials also.
Post a Comment