JavaFX Combobox row labeled ImageView +

I have a combobox that is populated with a list of objects of class Ejercicio (Ejercicio contains id, name ..).

ComboBox lstPrueba=new ComboBox<Ejercicio>();
//I get a list of Ejercicio from DAO an add all to the combobox
lstPrueba.getItems().addAll(new EjercicioDAO().obtenListaEjercicios("something"));

      

I need all combobox lines to have an ImageView (the png image names are based on the Ejercicio id because the png name is the Ejercicio id) plus a label (named Ejercicio).

I am trying but not working:

    class FormatoCelda extends ListCell<Ejercicio> {

    @Override
    public void updateItem(Ejercicio item, boolean empty) {
        super.updateItem(item, empty);
        Image img = new Image(getClass().getResourceAsStream("/img/"+String.valueOf(item.getId() + ".png")));
        ImageView imgv = new ImageView(img);
        if (item != null) {
            setGraphic(imgv);
            setText(item.getNombre());

        } else {
            setGraphic(null);
        }
    }
}

      

And in the initialization method:

    lstPrueba = new ComboBox<>();
    lstPrueba.getItems().addAll(new EjercicioDAO().obtenListaEjercicios("something"));

    lstPrueba.setCellFactory(new Callback<ListView<Ejercicio>, ListCell<Ejercicio>>() {
        @Override
        public ListCell<Ejercicio> call(ListView<Ejercicio> list) {
            return new FormatoCelda();
        }
    }
    );

      

I am new to JavaFX. I browse through the internet and StackOverFlow but I can't seem to find a good example for populating a control list type like combobox with the image + tag.

Sorry my english is not very good.

+3


source to share





All Articles