How to add elements to a ObservableList in JavaFx?
-
I am currently doing a form where I want to store the registration of a student of any institution, in that form I have a ComboBox that is the "group" and in that Combo would like to store information from the database on that table.
Group model
package Modelo;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;@Entity(name="grupo")
public class Grupo {@Id @GeneratedValue private int idGrupo; @Column(name="NombreG") private String nombre; @Column(name="CantidadA") private int cantidad; @Column(name="Grado") private String grado; @Column(name="Nivel") private float nivel; @Column(name="Nota") private String nota; @Column(name="Fecha") private Date fecha; @Column(name="IdPeriodo") private int id_periodo; public Grupo(int idGrupo, String nombre, int cantidad, String grado, float nivel, String nota, Date fecha, int id_periodo) { super(); this.idGrupo = idGrupo; this.nombre = nombre; this.cantidad = cantidad; this.grado = grado; this.fecha = fecha; this.nivel = nivel; this.nota = nota; this.id_periodo = id_periodo; } public Grupo() { this(0,"",0,"",0.0f,"",new Date(),0); } public int getIdGrupo() { return idGrupo; } public void setIdGrupo(int idGrupo) { this.idGrupo = idGrupo; } public String getNombre() { return nombre; } public void setNombre(String nombre) { this.nombre = nombre; } public int getCantidad() { return cantidad; } public void setCantidad(int cantidad) { this.cantidad = cantidad; } public String getGrado() { return grado; } public void setGrado(String grado) { this.grado = grado; } public float getNivel() { return nivel; } public void setNivel(float nivel) { this.nivel = nivel; } public String getNota() { return nota; } public void setNota(String nota) { this.nota = nota; } public Date getFecha() { return fecha; } public void setFecha(Date fecha) { this.fecha = fecha; } public int getId_periodo() { return id_periodo; } public void setId_periodo(int id_periodo) { this.id_periodo = id_periodo; }
}
Group Controller
@FXML
private ComboBox<Grupo> cmbGrupo;
private ObservableList<Grupo> grupos;cmbGrupo = new ComboBox<>();
grupos = FXCollections.observableArrayList();
cmbGrupo.setItems(grupos);
By compiling the program I get the following:
-
To add elements to the ComboBox you must use the setItems(...) method by indicating the elements it will contain, you can see a good example of how to use this control here: http://acodigo.blogspot.com/2015/04/javafx-uso-de-combobox.html