necesito anidar dos combobox con un mismo arraylist la idea es sacar un combobox con el listado de las especialidades de los doctores y otro combobox anidado con la idea de que al seleccionar el combobox de las especialidades me muestre en el combobox de los doctores aquellos que tienen esa especialidad.
el metodo para que no se repitan las especialidades ya esta configurado, a no ser que haya que hacerlo de otra manera
Hola, necesito anidar dos combobox con un mismo arraylist la idea es sacar un combobox con el listado de las especialidades de los doctores y otro combobox anidado con la idea de que al seleccionar el combobox de las especialidades me muestre en el combobox de los doctores aquellos que tienen esa especialidad.
el metodo para que no se repitan las especialidades ya esta configurado, a no ser que haya que hacerlo de otra manera
public class Ejerciciocombo extends javax.swing.JFrame {
ArrayList<Doctor> listadoctores;
public Ejerciciocombo() {
initComponents();
this.setLocationRelativeTo(null);
this.setTitle("prueba de combo");
iniciarDoctores();
//MostrarDoctores();
cargarEspecialidad();
}
public void iniciarDoctores() {
int i;
int[] codigo = {1, 2, 3, 4, 5};
String[] nombredoctor = {"Elena Diaz Lopez", "Juan Ruiz Luque", "Marta Jaén García", "Ana Perez Martin", "José Guerrero Castro"};
String[] especialidad = {"Medicina General", "Medicina General", "Pediatria", "Pediatria", "Cirugia"};
int[] numconsulta = {1, 3, 2, 4, 5};
Doctor nuevoDoctor;
listadoctores = new ArrayList<Doctor>();
for (i = 0; i < codigo.length; i++) {
nuevoDoctor = new Doctor(codigo[i], especialidad[i], nombredoctor[i], numconsulta[i]);
listadoctores.add(nuevoDoctor);
}
}
public void MostrarDoctores() {
int i;
String nombredoctor;
for (i = 0; i < listadoctores.size(); i++) {
nombredoctor = listadoctores.get(i).getNombredoctor();
cmbCombo2.addItem(nombredoctor);
}
}
public void cargarEspecialidad(){
Set<String> especialidadRepetida = new HashSet<>();
for (int i = 0; i <listadoctores.size(); i++){
especialidadRepetida.add(listadoctores.get(i).getEspecialidad());
}
for (String Especialidad : especialidadRepetida){
cmbCombo1.addItem(Especialidad);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
cmbCombo1 = new javax.swing.JComboBox<>();
cmbCombo2 = new javax.swing.JComboBox<>();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(cmbCombo1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
.addComponent(cmbCombo2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cmbCombo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmbCombo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(226, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Ejerciciocombo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Ejerciciocombo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Ejerciciocombo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Ejerciciocombo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ejerciciocombo().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> cmbCombo1;
private javax.swing.JComboBox<String> cmbCombo2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}