Help load ComboBox JavaFX FXML

0

Well I have a problem and I want to load a ComboBox with data returned by a database and here everything is fine. The thing is that when executing the method which would fill the ComboBox throw compile error.

This is the error: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

public class FXMLDocumentRegisterController implements Initializable {

    @FXML
    private JFXComboBox<String> CmbCountry;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        loadCmb();//aca ejecuto el metodo de carga del combobox
    }

    private void loadCmb(){//este metodo carga el combobox con los datos devueltos de la base de datos
        RepoPais repo = FabricaReposSQL.CrearRepoPais();
        Iterable<Pais> p = repo.FindAll();  for (Pais pais : p) {
            CmbCountry.getItems().add(pais.Nombre);     
        }
    }
}

The error occurs in the line where the Item is added to the ComboBox: CmbCountry.getItems().add(pais.Nombre);

I hope you can help me thank you very much:)

    
asked by Joaquin gonzalez 16.12.2016 в 20:03
source

1 answer

-1

a possible solution could be this. First, you must load the countries in an ObservableList. Something like this:

private ObservableList<String> paises = FXCollection.observableArrayList();

You add the countries you want to the observable:

paises.add("España");
paises.add("Francia");

And once you have the observable loaded with the countries you can do a generic method that loads any combobox with any observable. Something like this:

public static void loadCombo(ComboBox<String> combo, ObservableList<String> paises) { 

    if (null != combo) {
       combo.setItems(paises);
    } 
}

Once this is created, we just have to use it:

Util.loadCombo(comboBoxPaises,paises);

I hope it serves you, and sorry for not being able to respond properly.

Greetings.

    
answered by 25.09.2017 в 17:38