0

I'm new to Java and POO so I'm struggling this proyect small proyect I have to hand in to the Univ.

My main issue is that I using an atributte from an arraylist to populate the JComboBox, but when I add another object to that list in the other panel I cannot see the new value in the combobox. I can only see the new values if I re-run the proyect. How Can I do to update the JComboBox ass soon as I add values.

Can someone give me some advice about my issue? I know that my code is a mess.

public class Registrar_turno extends JPanel {

    private JComboBox combomatricula;
    private JPanel drop_down;
    private JPanel botonera;
    private FrameManager manager;

    private JLabel Nnombre;
    private JTextField nombre;
    private JLabel Aapellido;
    private JTextField apellido;
    private JLabel matricula;
    private JButton volver;

    private List od_nombres;
    private List od_apellidos;
    private Object nombre_resultado;
    private Object apellido_resultado;

    List<Odontologo> listaa = ObtenerLista();

    public Registrar_turno(FrameManager manager) {
    this.manager = manager;
    }

    public void Combotest(){
        this.setLayout(new BorderLayout());
        LayoutManager layout = new SpringLayout();
        drop_down = new JPanel();
        botonera = new JPanel();
        drop_down.setLayout(layout);

        Populate_matCombo();

        Nnombre = new JLabel("Nombre: ");
        nombre = new JTextField(10);
        nombre.setEditable(false);

        Aapellido = new JLabel("Apellido: ");
        apellido = new JTextField(10);
        apellido.setEditable(false);

        volver= new JButton("Volver");

        drop_down.add(Nnombre);
        drop_down.add(nombre);
        drop_down.add(Aapellido);
        drop_down.add(apellido);

        botonera.add(volver);

        SpringUtilities.makeCompactGrid(drop_down,2,2);

        Populate_matCombo();
        add(matricula,BorderLayout.PAGE_START);
        add(combomatricula,BorderLayout.PAGE_START);
        add(drop_down,BorderLayout.CENTER);
        add(botonera,BorderLayout.PAGE_END);

        volver.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == volver){
                    manager.ShowMenuZero();
                }
            }
        });
    }

    public List RellenarCampo_nombre (){
        List <String> nombresOD = new ArrayList<String>();

        for (Odontologo od : listaa){
            nombresOD.add(od.getNombre());
        }
        return nombresOD;
    }

    public List RellenarCampo_apellido (){
        List <String> apellidosOD= new ArrayList<String>();
        for (Odontologo odp : listaa){
            apellidosOD.add(odp.getApellido());
        }

        return apellidosOD;
    }

    public void Populate_matCombo(){
        matricula = new JLabel("Matricula");
        List<Integer> o = new ArrayList<Integer>();
        combomatricula = new JComboBox();
                for (Odontologo od : listaa){
                        o.add(od.getMatricula());
        }

        combomatricula.setModel(new DefaultComboBoxModel(o.toArray()));

        od_nombres=RellenarCampo_nombre();
        od_apellidos=RellenarCampo_apellido();

        combomatricula.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == combomatricula){
                    nombre_resultado=od_nombres.get(combomatricula.getSelectedIndex());
                    nombre.setText(nombre_resultado.toString());
                    apellido_resultado=od_apellidos.get(combomatricula.getSelectedIndex());
                    apellido.setText(apellido_resultado.toString());
                }
            }
        });
    }

    private List<Odontologo> ObtenerLista() {
        OdontologoService odontologoService=new OdontologoService();
        return odontologoService.listar();
    }
}

So uhmm Obtenerlista() is the file InputStream

My FrameManager is:

public class FrameManager  {

private JFrame frame;
private LoginPanel login;
private ZeroMenu menu0;
private AdminPaciente menu_paciente;
private AdminOdontologo menu_odontologo;
private AltaFormulario paciente_alta;
private AltaOdontologo odontologo_alta;
private Registrar_turno turnero;

public FrameManager (){}

public void GenManager(){
    frame= new JFrame();
    login = new LoginPanel(this);
    menu0= new ZeroMenu(this);
    menu_paciente = new AdminPaciente(this);
    menu_odontologo = new AdminOdontologo(this);
    paciente_alta = new AltaFormulario(this);
    odontologo_alta = new AltaOdontologo(this);
    turnero = new Registrar_turno(this);

    frame.setTitle("");
    login.setLoginFormPanel();
    menu0.ShowMenu();
    menu_paciente.ShowAPaciente();
    menu_odontologo.ShowAOdontologo();
    paciente_alta.PacienteAltaForm();
    odontologo_alta.Odontologo_AltaForm();
    turnero.Combotest();

    frame.setVisible(true);
}

public void ShowFame(){
    frame.setVisible(true);
}

public void ShowMenuZero(){
    frame.getContentPane().removeAll();
    frame.setBounds(200,200,370,300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(menu0);
    frame.getContentPane().validate();
    frame.getContentPane().repaint();
    turnero.Populate_matCombo();
}

public void PacienteAlta (){
    frame.getContentPane().removeAll();
    frame.setBounds(200,200,500,450);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(paciente_alta);
    frame.getContentPane().validate();
    frame.getContentPane().repaint();
}

public void OdontologoAlta(){
    frame.getContentPane().removeAll();
    frame.setBounds(200,200,500,450);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(odontologo_alta);
    frame.getContentPane().validate();
    frame.getContentPane().repaint();
}

public void OdontologoAlta(Odontologo o){
    frame.getContentPane().removeAll();
    odontologo_alta.llenarAltaOdontologo(o);
    frame.setBounds(200,200,500,450);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(odontologo_alta);
    frame.getContentPane().validate();
    frame.getContentPane().repaint();
    turnero.revalidate();
}

public void RegistrarTurno (){
    frame.getContentPane().removeAll();
    frame.setBounds(200,200,500,450);
    frame.setLocationRelativeTo(null);
    frame.getContentPane().validate();
    frame.getContentPane().repaint();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(turnero);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SantGlez
  • 1
  • 1
  • 1
    *using an atributte from an arraylist to populate the JComboBox, but when I add another object to that list in the other panel I cannot see the new value in the combobox.* - get rid of the ArrayList. The object should be added to the model of the combo box. If you want to make changes, you change the model, not the list. – camickr Nov 13 '21 at 16:56
  • In future, please add a [mre] that focuses on the immediate problem of adding elements to a combo box. Also, take care to use standard Java nomenclature - methods start with a lower case letter. – Andrew Thompson Nov 14 '21 at 00:02

1 Answers1

0

I was able to solve it calling ObtenerLista() inside the other methods, because the way it was coded ObtenerLista () was being used just once at the very beginnig. And the list was not being updated

SantGlez
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 15 '21 at 19:31