我有以下代码:
@FXML
private DatePicker birthday;
//other code
private final ChangeListener<Person> personListener = (value, oldValue, newValue) -> {
//other code
birthday.valueProperty().unbindBidirectional(oldValue.getBirthday());
//other code
};
生日属性的类型为java.time。LocalDate,并且属于类Person。因为我使用JPA,所以我不想使用JavaFX属性。上述代码无法编译。编译器的错误消息是:
error: no suitable method found for unbindBidirectional(LocalDate)
birthDayPicker.valueProperty().unbindBidirectional(oldV.getBirthday());
method Property.unbindBidirectional(Property<LocalDate>) is not applicable
(argument mismatch; LocalDate cannot be converted to Property<LocalDate>)
method ObjectProperty.unbindBidirectional(Property<LocalDate>) is not applicable
(argument mismatch; LocalDate cannot be converted to Property<LocalDate>)
我该如何解决这个问题?
更新:我的个人类具有以下代码:
@Entity
@Table(name = "PERSON")
@NamedQueries({
@NamedQuery(name = "Person.findAll", query = "SELECT p FROM Person p"),
@NamedQuery(name = "Person.findById", query = "SELECT p FROM Person p WHERE p.id = :id"),
@NamedQuery(name = "Person.findByFirstname", query = "SELECT p FROM Person p WHERE p.firstname = :firstname"),
@NamedQuery(name = "Person.findByLastname", query = "SELECT p FROM Person p WHERE p.lastname = :lastname"),
@NamedQuery(name = "Person.findByMail", query = "SELECT p FROM Person p WHERE p.mail = :mail"),
@NamedQuery(name = "Person.findByBirthday", query = "SELECT p FROM Person p WHERE p.birthday = :birthday")})
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Basic(optional = false)
@Column(name = "FIRSTNAME")
private String firstname;
@Column(name = "LASTNAME")
private String lastname;
@Column(name = "MAIL")
private String mail;
@Column(name = "BIRTHDAY")
@Temporal(TemporalType.DATE)
private LocalDate birthday;
public Person() {
}
public Person(Integer id) {
this.id = id;
}
public Person(Integer id, String firstname) {
this.id = id;
this.firstname = firstname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
Integer oldId = this.id;
this.id = id;
listenerList.firePropertyChange("id", oldId, id);
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
String oldFirstName = this.firstname;
this.firstname = firstname;
listenerList.firePropertyChange("firstname", oldFirstName, firstname);
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
String oldLastName = this.lastname;
this.lastname = lastname;
listenerList.firePropertyChange("mail", oldLastName, lastname);
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
String oldMail = this.mail;
this.mail = mail;
listenerList.firePropertyChange("mail", oldMail, mail);
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
LocalDate oldBirthDay = this.birthday;
this.birthday = birthday;
listenerList.firePropertyChange("birthday", id, birthday);
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Person)) {
return false;
}
Person other = (Person) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "jpa.entities.Person[ id=" + id + " ]";
}
@Transient
final private PropertyChangeSupport listenerList = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
listenerList.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
listenerList.removePropertyChangeListener(listener);
}
}
控制器类:
public class PersonController implements Initializable {
@FXML
private TableView<Person> personsTable;
@FXML
private TableColumn<Person, Integer> idColumn;
@FXML
private TableColumn<Person, String> firstColumn;
@FXML
private TableColumn<Person, String> lastColumn;
@FXML
private TableColumn<Person, LocalDate> birthdayColumn;
@FXML
private TableColumn<Person, String> mailColumn;
@FXML
private TextField id;
@FXML
private TextField firstName;
@FXML
private TextField lastName;
@FXML
private TextField mail;
@FXML
private DatePicker birthDayPicker;
private EntityManagerFactory emf;
private EntityManager em;
private ObservableList<Person> data;
private LocalDate birthday;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
emf = Persistence.createEntityManagerFactory("persistenceTest");
em = emf.createEntityManager();
data = FXCollections.observableArrayList();
birthDayPicker.setOnAction((ActionEvent evnt) -> {
birthday = birthDayPicker.getValue();
});
personsTable.getSelectionModel().selectedItemProperty().addListener(personListener);
configureColumn();
populate();
}
@FXML
private void addPerson(ActionEvent event) {
em.getTransaction().begin();
Person p = new Person(Integer.parseInt(id.getText()), firstName.getText());
p.setLastname(lastName.getText());
p.setBirthday(birthDayPicker.getValue());
p.setMail(mail.getText());
em.persist(p);
data.add(p);
em.getTransaction().commit();
}
@FXML
private void savePerson(ActionEvent event) {
}
@FXML
private void deletePerson(ActionEvent event) {
em.getTransaction().begin();
Person p = personsTable.getSelectionModel().selectedItemProperty().getValue();
data.remove(p);
em.remove(p);
em.getTransaction().commit();
}
private void populate() {
TypedQuery<Person> query = em.createQuery(
"SELECT e FROM Person e", jpa.entities.Person.class);
List<Person> list = query.getResultList();
data.addAll(list);
personsTable.setItems(data);
}
private void configureColumn() {
idColumn.setCellValueFactory(new PropertyValueFactory<Person, Integer>("id"));
firstColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("firstname"));
lastColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("lastname"));
birthdayColumn.setCellValueFactory(new PropertyValueFactory<Person, LocalDate>("birthday"));
birthdayColumn.setCellFactory(p -> {
return new TableCell<Person, LocalDate>() {
@Override
protected void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
final DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
setText(item.format(format));
}
}
};
});
mailColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("mail"));
}
private final ChangeListener<Person> personListener = (value, oldV, newV) -> {
if (oldV != null) {
id.textProperty().unbindBidirectional(oldV.getId());
firstName.textProperty().unbindBidirectional(oldV.getFirstname());
lastName.textProperty().unbindBidirectional(oldV.getLastname());
birthDayPicker.valueProperty().unbindBidirectional(oldV.getBirthday()); // error
mail.textProperty().unbindBidirectional(oldV.getMail());
}
if (newV != null) {
try {
id.textProperty().bindBidirectional(JavaBeanIntegerPropertyBuilder.create().bean(newV).name("id").build(), new NumberStringConverter());
firstName.textProperty().bindBidirectional(JavaBeanStringPropertyBuilder.create().bean(newV).name("firstname").build());
lastName.textProperty().bindBidirectional(JavaBeanStringPropertyBuilder.create().bean(newV).name("lastname").build());
birthDayPicker.valueProperty().bindBidirectional(); // error
mail.textProperty().bindBidirectional(JavaBeanStringPropertyBuilder.create().bean(newV).name("mail").build());
} catch (NoSuchMethodException e) {
System.out.println("erreur : " + e.getMessage());
}
}
};
}
更改 Person 类中的日期类型,然后添加并使用适当的访问器。
public class Person {
final private ObjectProperty<LocalDate> birthday;
public Person(LocalDate birthday) {
this.birthday = new SimpleObjectProperty<LocalDate>(birthday);
}
public LocalDate getBirthday() {
return birthday.get();
}
public void setBirthday(LocalDate birthday) {
this.birthday.set(birthday);
}
public ObjectProperty<LocalDate> birthdayProperty() {
return birthday;
}
}
. . .
@FXML
private DatePicker birthday;
//other code
private final ChangeListener<Person> personListener = (value, oldValue, newValue) -> {
//other code
birthday.valueProperty().unbindBidirectional(oldValue.birthdayProperty());
//other code
};
最简单的方法是在 Person
类中使用 JavaFX 属性。只要您使用“属性访问”而不是“字段访问”,这适用于 JPA。即:
public class Person {
private final StringProperty firstname = new SimpleStringProperty();
public StringProperty firstnameProperty() {
return firstname ;
}
@Basic(optional = false)
@Column(name = "FIRSTNAME")
public final String getFirstname() {
return firstnameProperty().get();
}
public final void setFirstname(String firstname) {
firstnameProperty().set(firstname);
}
private final ObjectProperty<LocalDate> birthday = new SimpleObjectProperty<>();
public ObjectProperty<LocalDate> birthdayProperty() {
return birthday ;
}
@Column(name="BIRTHDAY")
@Temporal(TemporalType.DATE)
public LocalDate getBirthday() {
return birthdayProperty().get();
}
public void setBirthday(LocalDate birthday) {
birthdayProperty().set(birthday);
}
// etc
}
(请注意,Hibernate仍然反对final
get
和set
方法,因此如果使用Hibernate,则必须将这些方法设置为非final,这稍微不太理想。如果使用符合JPA的ORM,则不会造成问题。)
如果由于某种原因无法在实体中使用 JavaFX 属性,则可以使用 JavaBeanObjectProperty
管理绑定。遵循以下行的代码应该可以工作:
public class PersonController {
private JavaBeanObjectProperty birthdayPropertyAdapter ;
// ...
private final ChangeListener<Person> personListener = (value, oldV, newV) -> {
if (oldV != null) {
id.textProperty().unbindBidirectional(oldV.getId());
firstName.textProperty().unbindBidirectional(oldV.getFirstname());
lastName.textProperty().unbindBidirectional(oldV.getLastname());
mail.textProperty().unbindBidirectional(oldV.getMail());
}
if (birthdayPropertyAdapter != null) {
birthdayPicker.valueProperty().unbindBidirectional(birthdayPropertyAdapter);
}
if (newV != null) {
try {
id.textProperty().bindBidirectional(JavaBeanIntegerPropertyBuilder.create().bean(newV).name("id").build(), new NumberStringConverter());
firstName.textProperty().bindBidirectional(JavaBeanStringPropertyBuilder.create().bean(newV).name("firstname").build());
lastName.textProperty().bindBidirectional(JavaBeanStringPropertyBuilder.create().bean(newV).name("lastname").build());
mail.textProperty().bindBidirectional(JavaBeanStringPropertyBuilder.create().bean(newV).name("mail").build());
birthdayPropertyAdapter = JavaBeanObjectPropertyBuilder.create()
.bean(newV)
.name("birthday")
.build();
birthdayPicker.valueProperty().bindBidirectional(birthdayPropertyAdapter);
} catch (NoSuchMethodException e) {
System.out.println("erreur : " + e.getMessage());
}
}
};
我试图将子类属性绑定到GridViewColumn。我有一个母类M1和三个不同的子类S1、S2和S3。GridViewColumn由类M1的对象填充。我希望将S2的一个属性绑定到这个GridViewColumn的头,而M1中没有实现这个属性。 有人能给我解释一下怎么做吗?
我知道如果我把属性放在。yml文件如下: 我可以将它们绑定到java。util。列出或设置类型。如果yaml属性如下: 我可以把它绑在地图上。我想知道是否可以将yml属性绑定到映射
我正在使用Guice开发一个小型web框架。我有一个Router对象,一旦初始化,它就会公开一个getControllerClasses()方法。我必须循环所有这些动态返回的类,以使用Guice绑定它们。 我绑定路由器: 但是,我如何在一个模块中获得绑定的路由器实例,以便也可以绑定其getControllerClasses()方法返回的类? 我能够在模块中获取路由器实例的唯一方法是,将该实例绑定到
我得到了这个错误 属性内的内插已被删除。改用v-bind或冒号速记。例如,代替
servlet 可以按名称绑定对象属性到 HttpSession 实现,任何绑定到会话的对象可用于任意其他的 servlet,其属于同一个 ServletContext 且处理属于相同会话中的请求。 一些对象可能需要在它们被放进会话或从会话中移除时得到通知。这些信息可以从 HttpSessionBindingListener 接口实现的对象中获取。这个接口定义了以下方法,用于标识一个对象被绑定到会
我在视图中定义了一个剑道下拉列表,如下所示: 我的模型有一个