Select multiple alternate rows in a JTable "getSelectedRows ();"

1

Good morning, I hope we can learn from the topic that I am trying to solve, but that I have stuck and I can not solve it.

Hopefully we can all learn in this beautiful forum.

I have the following line of code in which I insert the data of the selected row in the text field.

the following is in a jpopupmenu,

 int fila = tbfacturas.getSelectedRow();

if (fila >= 0) {
    txtodc.setText(tbfacturas.getValueAt(fila, 0).toString());
    txtfactura.setText(tbfacturas.getValueAt(fila, 1).toString());

} else {
    JOptionPane.showMessageDialog(null, "no seleciono fila");

}

I want you to make multiple selections and instead of loading the data into a text field, invoking the method to insert them into the database, only I understand that

A JTable by default works in MULTIPLE_INTERVAL_SELECTION mode if you do not change it. The following modes exist:

  

ListSelectionModel.SINGLE_SELECTION - You can select only one index   ListSelectionModel.SINGLE_INTERVAL_SELECTION - you can select consecutive indexes   ListSelectionModel.MULTIPLE_INTERVAL_SELECTION - you can select without restriction

What happens is that I use table.getSelectedRow () that only returns the number of the last selected row. what I'm looking for is:

int[] seleccionados = tabla.getSelectedRows();

You can help me-support me with this method, how to invoke it and even though at this moment you return them to me in a text field it will help me a lot, to pass this bump.

Thank you very much for everything before anything, and hopefully in the future I can share this knowledge with more people.

    
asked by Eduardo Castillo 19.07.2018 в 20:06
source

1 answer

0

Good afternoon Eduardo!

If I have not misunderstood your query, you can get the position vector seleccionados , which means that in that variable there is a one-dimensional array containing all the selected rows, such as [4,5,6,7] .

It would only take you to build a loop and run it this way.

for (int i= 0; i< seleccionados.length; i++){
    // usas el indice seleccionados[i] para indicar qué fila debes tratar
} 

Inside the for you make the insertion. And the same insertion should be done four times with a different row.

I hope I helped you

Greetings!

    
answered by 19.07.2018 в 22:07