I need to extend AbstractTableModel to represent some data in a table. I have a class Car
which should represent one item (row) in a table:
public class Car implements Comparable<Car> {
public String make;
public int year;
public double engineVol;
public double maxSpeed;
// ...getters/setters for the fields...
public Car (String make, int year, double engineVol, double maxSpeed) {
this.make = make;
this.year = year;
this.engineVol = engineVol;
this.maxSpeed = maxSpeed;
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
Car car = (Car) other;
if (year != car.year) return false;
if (Double.compare(car.engineVol, engineVol) != 0) return false;
if (Double.compare(car.maxSpeed, maxSpeed) != 0) return false;
return make.equals(car.make);
}
@Override
public int hashCode() {
int result;
long temp;
result = make.hashCode();
result = 31 * result + year;
temp = Double.doubleToLongBits(engineVol);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(maxSpeed);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public int compareTo(Car other) {
return this.make.compareTo(other.make);
}
}
These Car
objects are stored in a HashSet
, which resides in the CarTableModel
:
public class CarTableModel extends AbstractTableModel {
private static final long serialVersionUID = 7927259757559420606L;
private HashSet<Car> cars;
public CarTableModel(HashSet<Car> cars) {
this.cars = cars;
}
@Override
public int getRowCount() {
return cars.size();
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return null;
}
}
As far as I get it I need to override at least 3 methods in an AbstractTableModel. How do I override getValueAt for a HashSet? What are those rowIndex and columnIndex arguments concerning the HashSet? How are those indeces are applied to the HashSet if we cannot get values from one by an index? Is it possible at all?
P.S. It's not my caprice to use a HashSet here, it's a university assignment, so it has to go this way.