Load a JList with data from an array

1

Hi, I have my class Gestor with the following code:

Class gestor 
{

        private final String cadena = "jdbc:sqlserver://localhost:1433;databaseName=Persona";
        public final String usuario = "mi_usuario";
        public final String contrasenia = "mi_contraseña";


        ArrayList<Persona> cargarPersonas()
        {
            ArrayList<Persona> personas = new ArrayList<>();

            try
            {
                Connection cn = DriverManager.getConnection(cadena, usuario, contrasenia);
                Statement st = cn.createStatement();
                ResultSet rs = st.executeQuery("SELECT * FROM Personas");

                while (rs.next())
                {
                    String nombre = rs.getString("nombre");
                    String apellido = rs.getString("apellido");
                    int edad = rs.getInt("edad");

                    Persona p = new Persona(nombre,apellido,edad);
                    personas.add(p);
                }

                rs.close();
                st.close();
                cn.close();

            }catch (SQLException e)
            {
                System.out.println("ERROR : " + e);
            }

            return personas;
        }

}

I have a JFrame called ListadoPersonas with a JList called lstPersonas . I would like to upload the same with the data of ArrayList .

    
asked by Lucas. D 23.08.2017 в 23:15
source

1 answer

1

I'm not familiar with ArrayList ... But if these work the same as an array / vector / array / array you could use a for to fill the list like this:

    //Debes crear un DefaultListModel
    DefaultListModel lista = new DefaultListModel();
    Jlist.setModel(lista);

    String[] vector = new String[6]; //Por ejemplo (Solo agrego esto para que veas que es un vector)

    for(int i = 0;i < vector.length;i++) {
        lista.addElement(vector[i]);
    }

If the ArrayList works just like the vectors you just have to adapt this example

    
answered by 12.09.2017 / 05:07
source