使用DateTime作为StringProperty

时间:2014-03-14 23:07:16

标签: javafx-2 tableview

我在我的模型中将DateTime字段定义为StringProperty以显示日期。我有几行数据库中的日期列为空,并定义了一个cellfactory,以所需的格式显示日期&空行为空。当我尝试更新其中一个空列时,我的问题就出现了。新日期没有出现。它适用于已存在日期值的行。

部分问题:

txtfld.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent t) {
            if (t.getCode() == KeyCode.ENTER) {
                if(txtfld.getText().length() == 0) {
                    commitEdit(null);
                } else {
                    commitEdit((new DateTime(txtfld.getText(),"dd/MM/yyyy")).toString());
                }
            } else if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        }
    });

我正在更新模型的部分:

col_Purchase_DT.setOnEditCommit(new EventHandler<CellEditEvent<Purchase, String>>() {
        @Override 
        public void handle(CellEditEvent<Purchase, String> tbl) {
             (tbl.getTableView().getItems().get(tbl.getTablePosition().getRow())).setDOB(tbl.getNewValue());
       }
   });

我已经弄明白,在用日期更新空单元格后,不会调用col_Purchase_DT.setOnEditCommit()。但是适用于非空单元格。我正在使用JodaTime作为日期时间。

enter image description here

我无法更新第二行。但它适用于第一和第二第三排。

任何指针都会有所帮助。

2 个答案:

答案 0 :(得分:1)

你似乎为此做了太多编码。有一个TextFieldTableCell类可用于创建可编辑单元格,它可以为您处理所有连线。这是一个例子,基于教程中的常用示例。我使用Java 8 java.time.LocalDate作为日期列,但同样的想法可以应用于JodaTime(我只是不熟悉API)。

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class TableWithEditableDateColumn extends Application {

    @Override
    public void start(Stage primaryStage) {

        final TableView<Person> table = new TableView<>();
        final TableColumn<Person, String> firstNameCol = createTableColumn("firstName", "First Name", String.class);
        final TableColumn<Person, String> lastNameCol = createTableColumn("lastName", "Last Name", String.class);
        final TableColumn<Person, LocalDate> birthdayCol = createTableColumn("birthday", "Birthday", LocalDate.class);

        final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");

        firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
        lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
        birthdayCol.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<LocalDate>() {

            @Override
            public String toString(LocalDate t) {
                if (t==null) {
                    return "" ;
                } else {
                    return dateFormat.format(t);
                }
            }

            @Override
            public LocalDate fromString(String string) {
                try {
                    return LocalDate.parse(string, dateFormat);
                } catch (DateTimeParseException exc) {
                    return null ;
                }
            }

        }));

        final ObservableList<Person> data =
        FXCollections.observableArrayList(
            new Person("Jacob", "Smith", LocalDate.parse("14/03/1975", dateFormat)),
            new Person("Isabella", "Johnson", LocalDate.parse("27/09/1982", dateFormat)),
            new Person("Ethan", "Williams", null),
            new Person("Emma", "Jones", LocalDate.parse("12/07/1979", dateFormat)),
            new Person("Michael", "Brown", LocalDate.parse("19/10/1984", dateFormat))
        );
        table.getColumns().addAll(firstNameCol, lastNameCol, birthdayCol);
        table.setItems(data);
        table.setEditable(true);

        final BorderPane root = new BorderPane();
        root.setCenter(table);

        final Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private <T> TableColumn<Person, T> createTableColumn(String property, String title, Class<T> type) {
        TableColumn<Person, T> col = new TableColumn<>(title);
        col.setCellValueFactory(new PropertyValueFactory<>(property));
        col.setEditable(true);
        col.setPrefWidth(100);
        return col ;
    }

    public static class Person {

        private final StringProperty firstName ;
        private final StringProperty lastName ;
        private final ObjectProperty<LocalDate> birthday ;

        public Person(String firstName, String lastName, LocalDate birthday) {
            this.firstName = new SimpleStringProperty(this, "firstName", firstName);
            this.lastName = new SimpleStringProperty(this, "lastName", lastName);
            this.birthday = new SimpleObjectProperty<>(this, "birthday", birthday);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String value) {
            firstName.set(value);
        }

        public StringProperty firstNameProperty() {
            return firstName;
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String value) {
            lastName.set(value);
        }

        public StringProperty lastNameProperty() {
            return lastName;
        }

        public LocalDate getBirthday() {
            return birthday.get();
        }

        public void setBirthday(LocalDate value) {
            birthday.set(value);
        }

        public ObjectProperty birthdayProperty() {
            return birthday;
        }

    }

    public static void main(String[] args) {
        launch(args);
    }

}

答案 1 :(得分:0)

如果对空值进行更新,则不会触发

onEditCommit。

以下是答案:http://www.wobblycogs.co.uk/index.php/computing/javafx/145-editing-null-data-values-in-a-cell-with-javafx-2