猿问

使用对象列表时设置选择框值

我正在尝试设置我的choiceBox 的值。


它在使用这样的普通字符串时有效:


choiceBox.getItems.setAll(FXCollections.observableArrayList("a","b","c"));

choiceBox.setValue("a");

但是在使用 Class 填充和设置choiceBox时它不会设置值(并且没有错误)


ObservableList<Course> items = FXCollections.observableArrayList();

items.add(Course.getAll());


choiceBox.getItems().setAll(items);

choiceBox.setValue(schedule.getCourse());

也尝试使用,shedule.getCourse().toString()因为choiceBox使用toString方法来显示课程。


我的对象的哪一部分ChoiceBox需要?


我的课程班级:


public class Course {


// Property Value Factory

public static final String PVF_NAME = "name";


private String name;


// == constructors ==


public Course(String name) {

    this.name = name;

}


// == public methods ==


@Override

public String toString() {

    return name;

}


public static Course fromString(String line) {

    return new Course(line);

}


// Getters & Setters


public String getName() {

    return name;

}


public void setName(String name) {

    this.name = name;

}

}


ibeautiful
浏览 146回答 1
1回答

湖上湖

您需要覆盖toString()对象的方法。在ChoiceBox将使用该值的选项列表。从那里,你需要选择的值ChoiceBox通过传递一个refernece所需Course从coursesList。下面是一个简单的 MCVE 来演示:课程.java:import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;public class Course {&nbsp; &nbsp; private StringProperty courseName = new SimpleStringProperty();&nbsp; &nbsp; public Course(String courseName) {&nbsp; &nbsp; &nbsp; &nbsp; this.courseName.set(courseName);&nbsp; &nbsp; }&nbsp; &nbsp; public String getCourseName() {&nbsp; &nbsp; &nbsp; &nbsp; return courseName.get();&nbsp; &nbsp; }&nbsp; &nbsp; public StringProperty courseNameProperty() {&nbsp; &nbsp; &nbsp; &nbsp; return courseName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCourseName(String courseName) {&nbsp; &nbsp; &nbsp; &nbsp; this.courseName.set(courseName);&nbsp; &nbsp; }&nbsp; &nbsp; // The ChoiceBox uses the toString() method of our object to display options in the dropdown.&nbsp; &nbsp; // We need to override this method to return something more helpful.&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return courseName.get();&nbsp; &nbsp; }}主.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 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage) {&nbsp; &nbsp; &nbsp; &nbsp; // Simple interface&nbsp; &nbsp; &nbsp; &nbsp; VBox root = new VBox(5);&nbsp; &nbsp; &nbsp; &nbsp; root.setPadding(new Insets(10));&nbsp; &nbsp; &nbsp; &nbsp; root.setAlignment(Pos.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; // Create the ChoiceBox&nbsp; &nbsp; &nbsp; &nbsp; ChoiceBox<Course> cbCourses = new ChoiceBox<>();&nbsp; &nbsp; &nbsp; &nbsp; // Sample list of Courses&nbsp; &nbsp; &nbsp; &nbsp; ObservableList<Course> coursesList = FXCollections.observableArrayList();&nbsp; &nbsp; &nbsp; &nbsp; // Set the list of Course items to the ChoiceBox&nbsp; &nbsp; &nbsp; &nbsp; cbCourses.setItems(coursesList);&nbsp; &nbsp; &nbsp; &nbsp; // Add the ChoiceBox to our root layout&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().add(cbCourses);&nbsp; &nbsp; &nbsp; &nbsp; // Now, let's add sample data to our list&nbsp; &nbsp; &nbsp; &nbsp; coursesList.addAll(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Course("Math"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Course("History"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Course("Science"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Course("Geography")&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; // Now we can select our value. For this sample, we'll choose the 3rd item in the coursesList&nbsp; &nbsp; &nbsp; &nbsp; cbCourses.setValue(coursesList.get(2));&nbsp; &nbsp; &nbsp; &nbsp; // Show the Stage&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setWidth(300);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setHeight(200);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(new Scene(root));&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }}结果如下:编辑要按Course名称选择一个,您将需要一个辅助方法来Course从coursesList.使用 Java 8 流 API:&nbsp; &nbsp; private Course getCourseByName(String name, List<Course> courseList) {&nbsp; &nbsp; // This basically filters the list based on your filter criteria and returns the first match,&nbsp; &nbsp; // or null if none were found.&nbsp; &nbsp; return courseList.stream().filter(course ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; course.getCourseName().equalsIgnoreCase(name)).findFirst().orElse(null);}之前的 Java 版本:&nbsp; &nbsp; private Course getCourseByName(String name, List<Course> courseList) {&nbsp; &nbsp; // Loop through all courses and compare the name. Return the Course if a match is found or null if not&nbsp; &nbsp; for (Course course : courseList) {&nbsp; &nbsp; &nbsp; &nbsp; if (name.equalsIgnoreCase(course.getCourseName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return course;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return null;}您现在可以使用 cbCourses.setValue(getCourseByName("History", coursesList));编辑#2:为了稳定kleopatra 的批评,我将发布一种更“正确”的方法来更新Course对象的显示值。虽然我认为toString()在大多数简单应用程序中覆盖没有任何问题,特别是如果您以这样一种方式设计它,即您的对象只需要一个字符串表示,我将在此处添加另一种方法。不要toString()直接在您的Course对象中覆盖该方法,而是在其ComboBox自身上设置转换器:&nbsp; &nbsp; cbCourses.setConverter(new StringConverter<Course>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String toString(Course object) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return object.getCourseName();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Course fromString(String string) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });然而,我确实认为这在非常简单的应用程序中是不必要的,并且在我自己的实际项目中不需要它。然而,这是“正确”的方式。
随时随地看视频慕课网APP

相关分类

Java
我要回答