第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

使用對象列表時(shí)設(shè)置選擇框值

使用對象列表時(shí)設(shè)置選擇框值

ibeautiful 2021-12-01 18:58:43
我正在嘗試設(shè)置我的choiceBox 的值。它在使用這樣的普通字符串時(shí)有效:choiceBox.getItems.setAll(FXCollections.observableArrayList("a","b","c"));choiceBox.setValue("a");但是在使用 Class 填充和設(shè)置choiceBox時(shí)它不會設(shè)置值(并且沒有錯(cuò)誤)ObservableList<Course> items = FXCollections.observableArrayList();items.add(Course.getAll());choiceBox.getItems().setAll(items);choiceBox.setValue(schedule.getCourse());也嘗試使用,shedule.getCourse().toString()因?yàn)閏hoiceBox使用toString方法來顯示課程。我的對象的哪一部分ChoiceBox需要?我的課程班級:public class Course {// Property Value Factorypublic static final String PVF_NAME = "name";private String name;// == constructors ==public Course(String name) {    this.name = name;}// == public methods ==@Overridepublic String toString() {    return name;}public static Course fromString(String line) {    return new Course(line);}// Getters & Setterspublic String getName() {    return name;}public void setName(String name) {    this.name = name;}}
查看完整描述

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é)果如下:

http://img1.sycdn.imooc.com//61a755800001ed6902960240.jpg

編輯


要按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)目中不需要它。然而,這是“正確”的方式。


查看完整回答
反對 回復(fù) 2021-12-01
  • 1 回答
  • 0 關(guān)注
  • 155 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號