在标题点击上排序表列?

时间:2015-02-19 10:25:44

标签: java vaadin

以下代码会产生此输出:

enter image description here

如果您尝试使用此代码,vaadin会自动知道如何对CARMAKE&点击标题时TOPSPEED。

PASSENGER& DRIVER未按标题点击排序。

我该怎么做? Table在哪里说“我不知道如何对PERSON类型进行排序,所以我不会在该标题中添加点击监听器”?

我能够扩展beamItemContainer&覆盖排序方法。然后我可以添加按钮到视图中对列进行排序。但我希望能够点击表格标题!

我曾经以为我可以添加TableHeaderClickListeners,但是当我覆盖beanItemContainer的排序时,为什么vaadin不能为我做呢?它已经为CARMAKE&极速。

@Override
public void init(VaadinRequest request) {

    BeanItemContainer<SportsCar> beans = new BeanItemContainer<SportsCar>(SportsCar.class);

    Person tom = new Person("Tom");
    Person dick = new Person("Dick");
    Person harry = new Person("Harry");

    beans.addBean(new SportsCar("ferrari",180.0,tom,dick));
    beans.addBean(new SportsCar("Aston Martin",165.0,harry,null));

    Table t = new Table();
    t.setContainerDataSource(beans);
    t.setSizeFull();
    this.setContent(t);

}

public class SportsCar {
    String carMake;
    double topspeed;
    Person driver;
    Person Passenger;

    public SportsCar(String carMake, double topspeed, Person driver, Person passenger) {
        this.carMake = carMake;
        this.topspeed = topspeed;
        this.driver = driver;
        this.Passenger = passenger;
    }

    /**
     * Get the carMake.
     * 
     * @return the carMake.
     */
    public String getcarMake() {
        return carMake;
    }

    /**
     * Set the carMake.
     * 
     * @param carMake
     *            the carMake.
     */
    public void setcarMake(String carMake) {
        this.carMake = carMake;
    }

    /**
     * Get the topspeed.
     * 
     * @return the topspeed.
     */
    public double getTopspeed() {
        return topspeed;
    }

    /**
     * Set the topspeed.
     * 
     * @param topspeed
     *            the topspeed.
     */
    public void setTopspeed(double topspeed) {
        this.topspeed = topspeed;
    }

    /**
     * Get the driver.
     * 
     * @return the driver.
     */
    public Person getDriver() {
        return driver;
    }

    /**
     * Set the driver.
     * 
     * @param driver
     *            the driver.
     */
    public void setDriver(Person driver) {
        this.driver = driver;
    }

    /**
     * Get the passenger.
     * 
     * @return the passenger.
     */
    public Person getPassenger() {
        return Passenger;
    }

    /**
     * Set the passenger.
     * 
     * @param passenger
     *            the passenger.
     */
    public void setPassenger(Person passenger) {
        Passenger = passenger;
    }

}

public class Person {

    String carMake;

    /**
     * Create a Demo.Person.
     */
    public Person(String carMake) {
        this.carMake = carMake;
    }

    /**
     * Get the carMake.
     * 
     * @return the carMake.
     */
    public String getcarMake() {
        return carMake;
    }

    /**
     * Set the carMake.
     * 
     * @param carMake
     *            the carMake.
     */
    public void setcarMake(String carMake) {
        this.carMake = carMake;
    }

    @Override
    public String toString(){
        return carMake;
    }
}

1 个答案:

答案 0 :(得分:2)

Java不知道如何比较对象本身。首先,您必须指定可以通过实现“Comparable”接口来比较这些对象。然后你需要覆盖compareTo方法,如下所示:

public int compareTo(Person p) {
    return getcarMake().compareTo(p.getcarMake());
}

这应该可以解决问题(我没试过它)。