我想说我有两个型号。老师和学生以及每个学生可以有一位教师(不是几位)。所以我想在学生窗格上有一个组合框,我可以选择一位老师。
两个模型也存储在数据库中,我只希望教师的数据库ID在学生的模型中,但在组合框中应该出现教师的名字。
此外,模型应该绑定到组合框,因此如果有人在组合框中更改了教师,那么(学生的)模型也应该刷新。使用textfields我可以将它们绑定到StringProperty对象,但在这种情况下,我需要将comboxbox项(Teacher.java)绑定到Student.java中的interger-property。
我还考虑过将老师模型作为我学生班级内的一个属性,但我认为这无济于事,因为我需要将组合框项目(teacher.java)与学生模型中的教师对象绑定但只能绑定属性对象。
Teacher.java
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Teacher {
private Integer databaseID;
private StringProperty name = new SimpleStringProperty();
private IntegerProperty age = new SimpleIntegerProperty();
public Teacher (Integer databaseID) {
// load data from database and fill into model
}
public void store() {
// write model to database
}
// getters and setters ...
@Override
public String toString() {
return name.get();
}
@Override
public boolean equals(Object obj) {
// compare databaseIDs ...
return true;
}
}
Student.java
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Student {
private Integer databaseID;
private StringProperty name = new SimpleStringProperty();
private Integer teacherID;
public Student (Integer databaseID) {
// load data from database and fill into model
}
public void store() {
// write model to database
}
// getters and setters ...
@Override
public String toString() {
return name.get();
}
@Override
public boolean equals(Object obj) {
// compare databaseIDs ...
return true;
}
}
Application.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestApplication extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Student student = new Student(4711);
ComboBox<Teacher> teachers = new ComboBox<Teacher>();
fillTeacherComboBox(teachers);
// TODO: select the teacher from the student by default
// FIXME: Binding student.databaseID <--> ComboBox-Item.databaseID
StackPane root = new StackPane();
root.getChildren().add(teachers);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private void fillTeacherComboBox(ComboBox<Teacher> teachers) {
// TODO: Load data from database and fill teacher combo box
}
}
答案 0 :(得分:3)
我会在这里(至少)对设计进行一次更改:在Teacher
对象中存储对Student
对象的引用(并使用JavaFX属性来执行此操作):
public class Student {
private Integer databaseID;
private StringProperty name = new SimpleStringProperty();
private ObjectProperty<Teacher> teacher = new SimpleObjectProperty<>();
public Student (Integer databaseID) {
// load data from database and fill into model
/* Just as an aside, it is really bad practice to include
database code in your domain objects as you suggest here.
You should create a separate class that manages the database
code (a DataAccessObject) and the domain objects Student and
Teacher should be completely agnostic as to the mechanism by
which they are persisted (or even whether they are persisted).*/
}
public void store() {
// write model to database
// see above comment.
}
// getters and setters ...
public ObjectProperty<Teacher> teacherProperty() {
return teacher ;
}
public final Teacher getTeacher() {
return teacherProperty().get();
}
public final void setTeacher(Teacher teacher) {
teacherProperty().set(teacher);
}
@Override
public String toString() {
return name.get();
}
@Override
public boolean equals(Object obj) {
// compare databaseIDs ...
return true;
}
}
现在你做了
Student student = ... ;
ComboBox<Teacher> teachers = new ComboBox<>();
// populate combo box...
teachers.valueProperty().bindBidirectional(student.teacherProperty());
答案 1 :(得分:0)
您可以尝试以下方法之一:(为我工作)
semesterBoxDay.selectionModelProperty().bind(semesterBox.selectionModelProperty());
当第一个一变一第二个变而诗经发生时,我知道这很奇怪,我不知道是否有相应的解释(它在这里像bindBidirectional()一样工作)
semesterBoxDay.valueProperty().bindBidirectional(semesterBox.valueProperty());
当我在这里使用bind()时,直到我更改了第一个值,我才能更改第二个值
(我认为因为2d one值将永远不会更改,除非您对其进行设置,但我不知道如果单击列表中的新项,为什么不更改)。如果有人对这些问题有答案,我希望他能写出来。