Help with jcombobox in Netbeans?

0

It happens that I need a combobox to filter the data by branches. If the click on an item shows me in a table the data of such branch. I'm using a text file as a database (so I was asked) I could only make it save and delete ... It would be very helpful if you could help me I'm using a method to save the data in a txt, save everything written in the jtable.

As much as I could get is through this code ...

Scanner entrada = null;

    String linea;

    int numeroDeLinea = 1;

    boolean contiene = false;

String texto = (String) jcomboSucursal.getSelectedItem();

    try {

        File f = new File("archivo. txt" );

        entrada = new Scanner(f);

        while (entrada.hasNext()) { //mientras no se llegue al final del fichero

               linea = entrada.nextLine();  

            if (linea.contains(texto)) {   //si la línea contiene el texto buscado se muestra por pantalla

                System.out.println("Linea " + numeroDeLinea + ": " + linea);

                contiene = true;

            }

            numeroDeLinea++; //se incrementa el contador de líneas

        }

        if(!contiene){ //si el archivo no contienen el texto se muestra un mensaje indicándolo

            System.out.println(texto + " no se ha encontrado en el archivo");

        }

    } catch (FileNotFoundException e) {

        System.out.println(e.toString());

    } catch (NullPointerException e) {

        System.out.println(e.toString() + "No ha seleccionado ningún archivo");

    } catch (Exception e) {

        System.out.println(e.toString());

    } finally {

        if (entrada != null) {

            entrada.close();

        }

    }

Although it only shows me by console and I want to filter the data and show them in my tablet

    
asked by Cristhian Coria 21.09.2018 в 13:50
source

1 answer

0

Hi Well in this case you can use the Changue event of the Jcombobox. First, we are going to modify our current method so that it searches through the text and the time it loads it in our table. This would be our method. Remember to declare your DefaultTableModel to use it here.

/*
Método que recibe como parámetro un String que seria nuestra sucursal en este caso.
*/
private void buscaText(String texto){
Scanner entrada = null;
String linea;
int numeroDeLinea = 1;
boolean contiene = false;
try {
    File f = new File("C:\textop.txt");
    entrada = new Scanner(f);
    while (entrada.hasNext()) { //mientras no se llegue al final del fichero
           linea = entrada.nextLine();  
        if (linea.contains(texto)) {   //si la línea contiene el texto buscado se muestra por pantalla
            System.out.println("Se econtro en la Linea " + numeroDeLinea + ": " + linea);
             contiene=true;
            ///cargamos la tabla con un nuevo metodo
            ///recorremos la linea encontrada y cargamos fila a la tabla
            ///cada registro que se encuentre se cargara a la tabla.
            String[] temp; 
            temp = linea.split(","); 

            for(int i=0;i<temp.length;i++){
                modelo.addRow(temp);
              System.out.println(temp[i]); 
           }

        }

        numeroDeLinea++; //se incrementa el contador de líneas

    }

    if(!contiene){ //si el archivo no contienen el texto se muestra un mensaje indicándolo
        System.out.println(texto + " no se ha encontrado en el archivo");
    }


} catch (FileNotFoundException e) {
    System.out.println(e.toString());
} catch (NullPointerException e) {
    System.out.println(e.toString() + "No ha seleccionado ningún archivo");
} catch (Exception e) {
    System.out.println(e.toString());
} finally {
    if (entrada != null) {
        entrada.close();
    }    

}


}

In the Combobox event.

private void jComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {                                           
    String Nombre= jComboBox.getSelectedItem().toString();
    ///obtenemos el nombre de la sucursal.
   ///ejecutamos nuestro nuevo método.
    buscaText(Nombre);
}

I hope I have helped you!

    
answered by 24.09.2018 в 14:16