在更改JTable中的值后,TreeSet不会自行排序

时间:2017-09-28 21:48:34

标签: java swing sorting jtable treeset

我有一个班级Driver

@XmlRootElement
public class Driver implements Comparable<Driver>{

    private static int nextId=0;
    private int id;
    private String name;
    private LocalDate birth;
    private LocalDate hireBegin;
    private LocalDate hireEnd;

    public Driver(){
        super();
    }
    public Driver(String name, LocalDate birth, LocalDate hireBegin, LocalDate hireEnd) {
        this.name = name;
        this.birth = birth;
        this.hireBegin = hireBegin;
        this.hireEnd = hireEnd;
        this.id = nextId;
        nextId++;
    }

     public Driver(String name, String birth,String hireBegin, String hireEnd) {
        this.name = name; 
        DateTimeFormatter formatter =DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
        this.birth = LocalDate.parse(birth,formatter);
        this.hireBegin = LocalDate.parse(hireBegin,formatter);
        this.hireEnd = LocalDate.parse(hireEnd,formatter);
        this.id = nextId;
        nextId++;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getBirth() {
        return birth.toString();
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBirth(LocalDate birth) {
        this.birth = birth;
    }

     public void setBirth(String birth) {
        this.birth = LocalDate.parse(birth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public void setHireBegin(LocalDate hireBegin) {
        this.hireBegin = hireBegin;
    }

    public void setHireBegin(String hireBegin) {
        this.hireBegin = LocalDate.parse(hireBegin, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public void setHireEnd(LocalDate hireEnd) {
        this.hireEnd = hireEnd;
    }

    public void setHireEnd(String hireEnd) {
        this.hireEnd = LocalDate.parse(hireEnd, DateTimeFormatter.ofPattern("yyyy-MM-dd"));;
    }

    public String getHireBegin() {
        return hireBegin.toString();
    }

    public String getHireEnd() {
        return hireEnd.toString();
    }

    @Override
    public String toString() {
        return "Driver{" + "id=" + id + ", name=" + name + ", birth=" + birth + ", hireBegin=" + hireBegin + ", hireEnd=" + hireEnd + '}';
    }

    @Override
    public int compareTo(Driver o) {
        int result=0;
        result=this.name.compareTo(o.getName());
        if(result==0)
            result = this.id-o.getId();

        return result;
    }

}

我有一个TreeSet司机:

private TreeSet<Driver> collDriver = new TreeSet<>();

问题是当我尝试更改JTable中的元素值时:

@Override
public void setValueAt(Object value, int row, int col){
try{
        Driver d = getDriverAtRow(row);
        switch(col){
            case 1: 
                d.setName(value.toString());
                break;
            case 2: 
                d.setBirth(value.toString());
                break;  
            case 3: 
                d.setHireBegin(value.toString());
                break;
            case 4: 
                d.setHireEnd(value.toString());
                break;  
            }       


        db.notifyObservers("change in jtable happened");
}
    catch(Exception ex){
        System.out.println(ex);
        //if(listener !=null){
            //listener.handleTableModelException(new EventCellException(this,"update of cell is doc"));
        //}
    }

TreeSet将更改元素的值,但不会对表本身进行排序。例如,我在这里打印了两次TreeSet,一次是在我更改值之前,一次是在我更改之后:

@Override
public void notifyObservers(Object msg){
    setChanged();
    super.notifyObservers(msg.toString());
    System.out.println(collDriver);
}

输出:

[Driver{id=1, name=aaa, birth=1111-11-11, hireBegin=1111-11-11, hireEnd=3333-11-14}, Driver{id=0, name=drivername, birth=1111-11-11, hireBegin=1111-11-11, hireEnd=3333-11-14}, Driver{id=2, name=ssss, birth=1111-11-11, hireBegin=1111-11-11, hireEnd=3333-11-14}, Driver{id=3, name=zzz, birth=1111-11-11, hireBegin=1111-11-11, hireEnd=3333-11-14}]
[Driver{id=1, name=aaa, birth=1111-11-11, hireBegin=1111-11-11, hireEnd=3333-11-14}, Driver{id=0, name=drivername, birth=1111-11-11, hireBegin=1111-11-11, hireEnd=3333-11-14}, Driver{id=2, name=ssss, birth=1111-11-11, hireBegin=1111-11-11, hireEnd=3333-11-14}, Driver{id=3, name=qqq, birth=1111-11-11, hireBegin=1111-11-11, hireEnd=3333-11-14}]

如您所见,TreeSet不再排序,但值已更改。原因是什么?

0 个答案:

没有答案