How do I fill an arraylist with several objects using the interface?

1

EDIT

I want to print the 11 players that the user types in the interface.

Player is an object with the data string dorsal, last name and position.
Gamer is where the whole interface you see below is.
Driver is listener where I add function to the show and add button
t1, t2, t3, t4 are the JTextfield
Player1 is the name of the Player type arraylist

No matter how the players are saved, I just need to know that so there is no problem in changing the code.

I enclose the code where I am filling everything.

    public void actionPerformed(ActionEvent eve) 
    { 
        try{
            if (eve.getActionCommand().contentEquals("Agregar"))
            {
                String dorsal= this.verEquipo.t1.getText();
                String nombre= this.verEquipo.t2.getText();
                String apellido= this.verEquipo.t3.getText();
                String posicion= this.verEquipo.t4.getText();

                jugador.setDorsal(dorsal);
                jugador.setNombre(nombre);
                jugador.setApellido(apellido);
                jugador.setPosicion(posicion);

                jugador1.add(jugador);

            }
            else if (eve.getActionCommand().contentEquals("Mostrar"))
            {

                imprimir();
            }
        }
        catch (Exception e){ e.printStackTrace();}
    }

    public void imprimir(){
        Integer size=jugador1.size();
         for(int i=0;i<jugador1.size();i++){
         System.out.println(jugador1.get(i).getDorsal());
         System.out.println(jugador1.get(i).getNombre());
         System.out.println(jugador1.get(i).getApellido());
         System.out.println(jugador1.get(i).getPosicion());
        }
    }
    
asked by Yeison Soto 29.11.2018 в 22:11
source

1 answer

0

You must create a new object to which you will add the player's properties in this way all the elements will be added to the ArrayList :

if (eve.getActionCommand().contentEquals("Agregar"))
            {
                String dorsal= this.verEquipo.t1.getText();
                String nombre= this.verEquipo.t2.getText();
                String apellido= this.verEquipo.t3.getText();
                String posicion= this.verEquipo.t4.getText();

                jugador = new Jugador(); //*Crea nuevo objeto.

                jugador.setDorsal(dorsal);
                jugador.setNombre(nombre);
                jugador.setApellido(apellido);
                jugador.setPosicion(posicion);

                jugador1.add(jugador);

            }

and when printing, the values of the players will be displayed correctly.

public void imprimir(){
    // Integer size=jugador1.size();
     for(int i=0;i<jugador1.size();i++){
     System.out.println(jugador1.get(i).getDorsal());
     System.out.println(jugador1.get(i).getNombre());
     System.out.println(jugador1.get(i).getApellido());
     System.out.println(jugador1.get(i).getPosicion());
    }
}
    
answered by 30.11.2018 / 19:42
source