vendredi 17 juin 2016

LocalDate not showing in TableColumn

I tried to set a column in a TableView like that:

private TableColumn<Person, LocalDate> colBirthday;

and in the initialize method i have assigned the value using lambdas

birthday.setCellValueFactory(cellData -> cellData.getValue().birthdayDataProperty());

when i try to initialize the object using the Constructor

public Person(String name) {
    this.name= new SimpleStringProperty(name);
    this.letturaData = new SimpleObjectProperty<LocalDate>(LocalDate.of(2000,10,10));
}

it will work, and i can see the date in the relative table column (this is the getter and the setter):

public LocalDate getBirthdayData() {
    return birthdayData.get();
}

public ObjectProperty<LocalDate> birthdayDataProperty() {
    if (birthdayData == null) birthdayData = new SimpleObjectProperty<LocalDate>(this, "birthdayData");
    return birthdayData;
}

public void setBirthdayData(LocalDate birthdayData) {
    this.birthdayDataProperty().set(birthdayData);
}

but when i try to assign it after creating the object:

private void addTestPerson() {
    Person person = new Person ("test");
    slot.setBirthdayDate(DateUtil.parse("20/05/2016"));
}

it will not work, where DateUtil is this class:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
 * Helper functions for handling dates.
 *
 * @author Marco Jakob
 */
public class DateUtil {

    /** The date pattern that is used for conversion. Change as you wish. */
    private static final String DATE_PATTERN = "dd/MM/yyyy";

    /** The date formatter. */
    private static final DateTimeFormatter DATE_FORMATTER =
            DateTimeFormatter.ofPattern(DATE_PATTERN);

    /**
     * Returns the given date as a well formatted String. The above defined
     * {@link DateUtil#DATE_PATTERN} is used.
     *
     * @param date the date to be returned as a string
     * @return formatted string
     */
    public static String format(LocalDate date) {
        if (date == null) {
            return null;
        }
        return date.format(DATE_FORMATTER).toString();
    }

    /**
     * Converts a String in the format of the defined {@link DateUtil#DATE_PATTERN}
     * to a {@link LocalDate} object.
     *
     * Returns null if the String could not be converted.
     *
     * @param dateString the date as String
     * @return the date object or null if it could not be converted
     */
    public static LocalDate parse(String dateString) {
        try {
            return LocalDate.parse(dateString,DATE_FORMATTER);
        } catch (DateTimeParseException e) {
            return null;
        }
    }

    /**
     * Checks the String whether it is a valid date.
     *
     * @param dateString
     * @return true if the String is a valid date
     */
    public static boolean validDate(String dateString) {
        // Try to parse the String.
        return DateUtil.parse(dateString) != null;
    }
}

Can someone explain me what i'm doing wrong and why? If i try to print the date to the stdout, using the getter, i see it, that means that's correctly setted or not?

Aucun commentaire:

Enregistrer un commentaire