Now that I managed to put the objects from a file into a ArrayList
, I have to display them into a JTable
.
These are the 3 objects contained in
Use a TableModel
for showing data in the JTable
. For Example:
In UI class, set the table model to the table.
JTable table = new JTable(new MonModel());
Table Model class
class MonModel extends AbstractTableModel {
private List l;
private String[] columnNames = {"Longitude", "Latitude"};
public MonModel() {
l = new ArrayList();
l.add(new LatNLon("45.573715", "-73.900295"));
l.add(new LatNLon("45.573715", "-73.900295"));
l.add(new LatNLon("45.573715", "-73.900295"));
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
public int getColumnCount() {
return 2;
}
public int getRowCount() {
return l.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(columnIndex==0){
return l.get(rowIndex).getX();
}
else if(columnIndex==1){
return l.get(rowIndex).getY();
}
return null;
}
}
Latitude and Longitude class.
class LatNLon {
private String x;
private String y;
public LatNLon(String x, String y) {
this.x = x;
this.y = y;
}
// Code: For Getters and Setters.
}
Also read How to use Tables.