gpt4 book ai didi

Javafx Combobox 获取选定索引返回 -1

转载 作者:行者123 更新时间:2023-12-02 12:11:41 29 4
gpt4 key购买 nike

我是 JavaFX 新手,每当我从 ComboBox 中选择一个项目时,我都会遇到这个奇怪的异常,我在数据库中有一堆记录,我正在提取这些记录并将其放入组合框作为字符串,问题是当我选择组合框中的项目时,我得到 ArrayIndexOutOfBoundsException: -1,有时我可以显示组合框中所需的信息,有时却不能,这是什么原因这个错误?我该如何解决它,我们将不胜感激?

这是组合框操作方法:

@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());
});

}

最佳答案

因为这是常见的 symptom原因有很多,单独检查可能会有所帮助。从看到的例子开始here ,下面的变体监听组合的 SelectionModel并在 TextField 中显示当前选择的值。请注意,组合的选定索引变为 -1当使用清除按钮清除组合的模型时。

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, …));

image

完整示例:

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);
}
}

关于Javafx Combobox 获取选定索引返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46501306/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com