JavaFX 控件 ChoiceBox

ChoiceBox

基礎使用

ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.getItems().addAll("item1", "item2", "item3");
//設置選中
choiceBox.setValue("item1");
//設置選中
choiceBox.getSelectionModel().select("item3");
//設置選擇監聽
choiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
    }
});
choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
    }
});

自定義對象

ChoiceBox<Student> choiceBox = new ChoiceBox<>();
choiceBox.setConverter(new StringConverter<Student>() {
    @Override
    public String toString(Student object) {
        return object.name;
    }

    @Override
    public Student fromString(String string) {
        System.out.println("string = " + string);
        return null;
    }
});
choiceBox.getItems().addAll(student1, student2, student3);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章