Perform random on a jList

0

I am doing a program that performs a raffle among all the participants, they go from a JTextArea to a JList. When I click on the 'Confirm' button, I pass the text of the JTextArea to the Jlist, deleting the text that is not a name, but when I click on the 'Sort' button, I do not get the draw on the JList, but on the textArea. I leave the code to be clearer. At the time of the draw I can not get it done with the names JList.

    private void botonConfirmarMouseClicked(java.awt.event.MouseEvent evt) {                                            

    // Bloquea el cuadro de texto

    jTextArea1.setEnabled(false);

    //  Crea un arreglo de String

    String[] textoConNombres = jTextArea1.getText().split("\n");


    DefaultListModel lista = new DefaultListModel();

    // Recorre el arreglo y lo llena con los nombres
    for(String nombre : textoConNombres) {
        lista.addElement(nombre);
        // Elimina el elemento si contiene alguna palabra que no sirve
        if (nombre.contains("Agregar a amigos")) {
            lista.removeElement(nombre);
        }
        if (nombre.contains("Me gusta")) {
            lista.removeElement(nombre);
        }
        if (nombre.contains(" en común")) {
            lista.removeElement(nombre);
        }
        if(nombre.contains("Amigo")){
            lista.removeElement(nombre);
        }
        if(nombre.contains("Amigos")){
            lista.removeElement(nombre);
        }
        if(nombre.contains("Seguir")){
            lista.removeElement(nombre);
        }
        if(nombre.contains("Te gusta")){
            lista.removeElement(nombre);
        }


    }


    jList1.setModel(lista);


}                                           

    private void botonSortearMouseClicked(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
    int cantTitulares = Integer.parseInt((String) 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(listaNombres.get(ganador));  // Obtiene el nombre de la lista según el entero elegido
        listaNombres.remove(ganador);
    }






    // Muestra el resultado en VentanaGanador
    VentanaGanador ventGanador = new VentanaGanador();
    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
    ventGanador.etiquetaGanador.setText(ganadoresStr); // ajusta el JLabel con todos los ganadores
    ventGanador.setVisible(true);






}        

I know I'm probably doing something wrong in the 'for' part with the 'random', but I do not know what.

    
asked by Martín 09.01.2018 в 18:56
source

1 answer

2

Greetings again, Martín.

Look at this line within the method botonSortearMouseClicked

String[] textoConNombres = jTextArea1.getText().split("\n");

There you are getting the values of jTextArea1 again. It should be with jList1 to which you assigned the values recently.

EDIT:

The way in which you will have to obtain the values, will no longer be with getText() , since the JList are made up of elementos and not by text strings.

Change these two lines:

String[] textoConNombres = jTextArea1.getText().split("\n");
List<String> listaNombres = new ArrayList<>(Arrays.asList(textoConNombres));

for this one:

DefaultListModel listaNombres = (DefaultListModel) jList1.getModel();

What you will do is get the modelo of that list with the elements.

    
answered by 09.01.2018 / 19:21
source