1 回答

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
您需要覆蓋toString()對象的方法。在ChoiceBox將使用該值的選項(xiàng)列表。
從那里,你需要選擇的值ChoiceBox通過傳遞一個(gè)refernece所需Course從coursesList。
下面是一個(gè)簡單的 MCVE 來演示:
課程.java:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Course {
private StringProperty courseName = new SimpleStringProperty();
public Course(String courseName) {
this.courseName.set(courseName);
}
public String getCourseName() {
return courseName.get();
}
public StringProperty courseNameProperty() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName.set(courseName);
}
// The ChoiceBox uses the toString() method of our object to display options in the dropdown.
// We need to override this method to return something more helpful.
@Override
public String toString() {
return courseName.get();
}
}
主.java:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// Create the ChoiceBox
ChoiceBox<Course> cbCourses = new ChoiceBox<>();
// Sample list of Courses
ObservableList<Course> coursesList = FXCollections.observableArrayList();
// Set the list of Course items to the ChoiceBox
cbCourses.setItems(coursesList);
// Add the ChoiceBox to our root layout
root.getChildren().add(cbCourses);
// Now, let's add sample data to our list
coursesList.addAll(
new Course("Math"),
new Course("History"),
new Course("Science"),
new Course("Geography")
);
// Now we can select our value. For this sample, we'll choose the 3rd item in the coursesList
cbCourses.setValue(coursesList.get(2));
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(200);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
結(jié)果如下:
編輯
要按Course名稱選擇一個(gè),您將需要一個(gè)輔助方法來Course從coursesList.
使用 Java 8 流 API:
private Course getCourseByName(String name, List<Course> courseList) {
// This basically filters the list based on your filter criteria and returns the first match,
// or null if none were found.
return courseList.stream().filter(course ->
course.getCourseName().equalsIgnoreCase(name)).findFirst().orElse(null);
}
之前的 Java 版本:
private Course getCourseByName(String name, List<Course> courseList) {
// Loop through all courses and compare the name. Return the Course if a match is found or null if not
for (Course course : courseList) {
if (name.equalsIgnoreCase(course.getCourseName())) {
return course;
}
}
return null;
}
您現(xiàn)在可以使用 cbCourses.setValue(getCourseByName("History", coursesList));
編輯#2:
為了穩(wěn)定kleopatra 的批評,我將發(fā)布一種更“正確”的方法來更新Course對象的顯示值。雖然我認(rèn)為toString()在大多數(shù)簡單應(yīng)用程序中覆蓋沒有任何問題,特別是如果您以這樣一種方式設(shè)計(jì)它,即您的對象只需要一個(gè)字符串表示,我將在此處添加另一種方法。
不要toString()直接在您的Course對象中覆蓋該方法,而是在其ComboBox自身上設(shè)置轉(zhuǎn)換器:
cbCourses.setConverter(new StringConverter<Course>() {
@Override
public String toString(Course object) {
return object.getCourseName();
}
@Override
public Course fromString(String string) {
return null;
}
});
然而,我確實(shí)認(rèn)為這在非常簡單的應(yīng)用程序中是不必要的,并且在我自己的實(shí)際項(xiàng)目中不需要它。然而,這是“正確”的方式。
添加回答
舉報(bào)