Show elements of ArrayList in another window

2

I'm doing a small application that makes a draw among several participants and shows the result in a JDialog. I have two errors in the code that do not allow it to execute well:

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
    //  Convierte el arreglo textoConNombres a una lista listaNombres
    String[] textoConNombres = jTextArea1.getText().split("\n");
    List<String> listaNombres = new ArrayList<>
    (Arrays.asList(textoConNombres));

    Random sorteo = new Random();

    //  Obtiene el valor del comboBox

    //No puedo castear de object a int en la parte de JComboBox1
    int cantTitulares = Integer.parseInt(jComboBox1.getSelectedItem());
    ArrayList ganadorTitular = new ArrayList();

    /*  Hace el sorteo las veces que dice el comboBox
        agrega al ganador a un arrayList aparte
        lo elimina de la lista para hacer el sorteo con el resto
        de los participantes que quedan sin ganar.*/

    for (int i = 0; i < cantTitulares; i++) {
        int ganador = sorteo.nextInt(listaNombres.size());
        ganadorTitular.add(ganador);
        listaNombres.remove(ganador);
    }



    // Muestra el resultado en VentanaGanador, que es otra clase

    VentanaGanador ventGanador = new VentanaGanador();
    for (int i = 0; i < ganadorTitular.size(); i++) {

        // Aquí no puedo seleccionar el índice del array para mostrar el elemento
        ventGanador.etiquetaGanador.setText(/////);

    }

The idea is basically that within the for the random will be made and the person who won will be removed so that they do not get drawn again. The problem is that I can not show the Array objects, I tried and searched but I do not know how.

    
asked by Martín 08.01.2018 в 19:11
source

1 answer

1

Greetings, Martín.

You see, the way a Object is converted (or 'castes') is different from how it is usually done with String , in your code you are using the second form:

Integer.parseInt(jComboBox1.getSelectedItem());

If you realize, this: jComboBox1.getSelectedItem() returns a value of type Object , then you have two options, or you make the whole model of JComboBox are always integer values (since they will be used for that), Or, you simply convert the Object returned to the whole.

To not enter into many details, you can use the second form. To achieve this you must do something like this:

int cantTitulares = (int) ((String) jComboBox1.getSelectedItem());

That way, you'll get Object of JComboBox as an integer.

On the other hand, you have the problem of showing on a label (I suppose you mean a JLabel ) the winner (s) generated. If what you want is to show the name of the winner (the ones you added in the listaNombres list) you must first make a change in the for where you choose the winners, why? Well, if we check your code:

for (int i = 0; i < cantTitulares; i++) {
    int ganador = sorteo.nextInt(listaNombres.size());
    ganadorTitular.add(ganador); // Aquí estás guardando un entero
    listaNombres.remove(ganador);
}

ganador is a integer , so keep the name of the winner would be:

for (int i = 0; i < cantTitulares; i++) {
    int ganador = sorteo.nextInt(listaNombres.size());
    ganadorTitular.add(listaNombres.get(ganador));  // Obtienes el nombre de la lista según el entero elegido
    listaNombres.remove(ganador);
}

That way, when you show them in etiquetaGanador , you would do this:

for (int i = 0; i < ganadorTitular.size(); i++) {
    ventGanador.etiquetaGanador.setText((String) ganadorTitular.get(i));
}

(String) ganadorTitular.get(i) what it does is convert the Object into String so that it can be added to JLabel as text. This is because in the ArrayList you did not indicate the data type and by default it stores them as Object .

EDIT:

Regarding your question in the comment on adding the other names, a really simple way and that allows you to keep everything just as you have it, is using code HTML within the text that you will put the JLabel etiquetaGanador .

Also, for this you should first make a small change in the for that you use to put the name of the winners in that JLabel .

It would have to stay something like this:

String ganadoresStr = "<html>"; // Se crea la cadena de texto con la etiqueta inicial <html>
for (int i = 0; i < ganadorTitular.size(); i++) {
    ganadoresStr += ganadorTitular.get(i); // agrega el primer ganador
    if (i + 1 < ganadorTitular.size()) { // verifica si existirán más ganadores
        ganadoresStr += "<br>"; // si existirán más, agrega salto de línea para agregar al otro debajo de él
    }
}
ganadoresStr += "</html>"; // finaliza la etiqueta
ventanaGanador.etiquetaGanador.setText(ganadoresStr); // ajusta el JLabel con todos los ganadores

Basically what it does is add the name of the winners in the same text string, and each winner separates it with the HTML tag <br> (which corresponds to a line break). The if within for , only verifies whether or not to add that label (it is to avoid adding a line break at the end). And finally you close the HTML tag with </html> .

In this way, the names of the winners would remain as a 'list' in the same JLabel without the need to modify your components.

    
answered by 08.01.2018 / 21:45
source