Java 将其添加到codehmm,错误为ArrayIndexOutOfBoundsException+您是说您可以查看组合框上的所有项目(至少在您选择一个项目之前)。我只能假设您在实际获取所选项目之前以某种方式删除了这些项目,或者以某种方式将selectedIte

Java 将其添加到codehmm,错误为ArrayIndexOutOfBoundsException+您是说您可以查看组合框上的所有项目(至少在您选择一个项目之前)。我只能假设您在实际获取所选项目之前以某种方式删除了这些项目,或者以某种方式将selectedIte,java,javafx,combobox,Java,Javafx,Combobox,将其添加到codehmm,错误为ArrayIndexOutOfBoundsException+您是说您可以查看组合框上的所有项目(至少在您选择一个项目之前)。我只能假设您在实际获取所选项目之前以某种方式删除了这些项目,或者以某种方式将selectedItem设置为-1。请确保在打印selectedIndex之前先打印recherche.getItem().size()。如果它为0,则需要查找数据插入的错误。如果没有,则搜索如果您正在某处设置selectedIndex=-1如果您可以发布问题的工作


将其添加到codehmm,错误为ArrayIndexOutOfBoundsException+您是说您可以查看组合框上的所有项目(至少在您选择一个项目之前)。我只能假设您在实际获取所选项目之前以某种方式删除了这些项目,或者以某种方式将selectedItem设置为-1。请确保在打印selectedIndex之前先打印recherche.getItem().size()。如果它为0,则需要查找数据插入的错误。如果没有,则搜索如果您正在某处设置selectedIndex=-1如果您可以发布问题的工作示例,我可以帮助您快速完成;)因为我可以看到整个画面。因为现在,例如,我看不出“clearFields()”等内部是否存在错误。旁白:为什么在事件处理程序中有
Platform.runLater(…)
?每当有时出现问题时,我都会想到不正确的同步。
@FXML
private ComboBox<String> recherche;

@FXML
private void Combo(ActionEvent event) {
        Platform.runLater(() -> {

            int selectedIndex = this.recherche.getSelectionModel().getSelectedIndex();
            System.out.println("**************selected intedx "+selectedIndex); //prints -1 and throws ArrayOutOfBoundsException

            ObservableList<String> listPayment = FXCollections
                    .observableArrayList(PaymentQueries.listOfPayamentDates(listOfData);
            recherche.setItems(listPayment);
            recherche.setEditable(true);
            //for autocompletion
            TextFields.bindAutoCompletion(recherche.getEditor(), recherche.getItems());

        });

    }
 public void reload() {
        Platform.runLater(() -> {

            clearFields();
            ObservableList<String> listEnfant = FXCollections.observableArrayList(EnfantQueries.getData().getNoms());
            recherche.setItems(listEnfant);
            recherche.setEditable(true);
            TextFields.bindAutoCompletion(recherche.getEditor(), recherche.getItems());
        });

    }
SingleSelectionModel selectionModel = comboBox.getSelectionModel();
TextField selection = new TextField(String.valueOf(selectionModel.getSelectedIndex()));
selectionModel.selectedIndexProperty().addListener((Observable o) -> {
    selection.setText(String.valueOf(selectionModel.getSelectedIndex()));
});
…
dialogPane.setContent(new VBox(8, textField, datePicker, comboBox, selection, …));
import java.time.LocalDate;
import java.util.Optional;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @see https://stackoverflow.com/a/46504588/230513
 * @see http://stackoverflow.com/q/44147595/230513
 * @see http://www.javaworld.com/article/2991463/
 */
public class DialogTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Dialog<Results> dialog = new Dialog<>();
        dialog.setTitle("Dialog Test");
        dialog.setHeaderText("Please specify…");
        DialogPane dialogPane = dialog.getDialogPane();
        dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
        TextField textField = new TextField("Name");
        DatePicker datePicker = new DatePicker(LocalDate.now());
        ObservableList<Venue> options =
            FXCollections.observableArrayList(Venue.values());
        ComboBox<Venue> comboBox = new ComboBox<>(options);
        SingleSelectionModel selectionModel = comboBox.getSelectionModel();
        selectionModel.selectFirst();
        TextField selection = new TextField(String.valueOf(selectionModel.getSelectedIndex()));
        selectionModel.selectedIndexProperty().addListener((Observable o) -> {
            selection.setText(String.valueOf(selectionModel.getSelectedIndex()));
        });
        Button clear = new Button("Clear");
        clear.setOnAction((action) -> {
            options.clear();
        });
        Button restore = new Button("Restore");
        restore.setOnAction((action) -> {
            options.clear();
            options.addAll(Venue.values());
            selectionModel.selectLast();
        });
        dialogPane.setContent(new VBox(8, textField, datePicker, comboBox, selection, clear, restore));
        Platform.runLater(textField::requestFocus);
        dialog.setResultConverter((ButtonType button) -> {
            if (button == ButtonType.OK) {
                return new Results(textField.getText(),
                    datePicker.getValue(), comboBox.getValue());
            }
            return null;
        });
        Optional<Results> optionalResult = dialog.showAndWait();
        optionalResult.ifPresent((Results results) -> {
            System.out.println(
                results.text + " " + results.date + " " + results.venue);
        });
    }

    private static enum Venue {Here, There, Elsewhere}

    private static class Results {

        String text;
        LocalDate date;
        Venue venue;

        public Results(String name, LocalDate date, Venue venue) {
            this.text = name;
            this.date = date;
            this.venue = venue;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}