How to extract data from multiple cells in a Jtable

1

I am trying to extract several data from several cells selected from a Jtable. The selection is free. I have only managed to extract a data every time I click on a cell. I have used the following commands from Jtable: MyTable.getSelectedRow and MyTable.getSelectedColumn .

Investigate and it came to me that for several data I use: MyTable.getSelectedRows and MyTable.getSelectedColumns .

At the moment of wanting to implement the commands that I investigated, I can not convert Int [] to Int . And therefore I can not extract data en masse.

This is my code:

public class Haxcell{

public JFrame ventana;
private JTable tabla;
private DefaultTableModel dtm;
private final JToolBar barra = new JToolBar();   
private final JTextField coord = new JTextField("Ingrese Coordenadas. Ej: (A1,A2)");
private final JLabel mostrar = new JLabel("Mostrar en:");
private final JTextField impresion = new JTextField("Ingrese Celda de Resultado. Ej: (A3)");
private final JButton envia = new JButton("envia");

private final String [][] datos={{"hola","","","","","","","","",""},{"","nepe","","","","","","","",""},{"","","","","","","","","",""}
                                ,{"","","","","","","","","",""},{"","","","","","","","","",""},{"","","","","","","","","",""}
                                ,{"","","","","","","","","",""},{"","","","","","","","","",""},{"","","","","","","","","",""}
                                ,{"","","","","","","","","",""}};
private final String [] cabezera={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
private final String [] opera = {"SUMA","RESTA","PRODUCTO","DIVISION","MAXIMO","MINIMO","PROMEDIO"};
private final    JComboBox listaoper = new JComboBox(opera);

//CONSTRUCTOR
public Haxcell(){//CONSTRUCTOR
    ventana = new JFrame("Haxcell");
    ventana.setLayout(new BorderLayout(30,30));//TIPO DE LAYOUT (SEPARACIONES)

    ventana.setSize(700,300);//TAMAÑO DE LA VENTANA

    seet_Tabla();
    seet_Toolbar();
    geet_Informacion(); 

    ventana.setLocationRelativeTo(null);//Centrar ventana
    ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ventana.setVisible(true);
    ventana.setIconImage(new ImageIcon(getClass().getResource("../Imagenes/logo.png")).getImage());//ICONO          
}
//FUNCION: CABEZERAS LATERALES, TABLA, SCROLL VERTICAL 
public final void seet_Tabla(){
    tabla = new JTable(datos,cabezera);

    JScrollPane Subirbajar = new JScrollPane(tabla);
    //Madre que permite seleccionar celdas
    tabla.setColumnSelectionAllowed(true);
    tabla.setRowSelectionAllowed(true);
    tabla.setCellSelectionEnabled(true);

    //Tamaño de Tabla
    Subirbajar.setPreferredSize(new Dimension(500,150));
    //Cabezeras laterales
    JTable filas = new RowNumberTable(tabla);
    Subirbajar.setRowHeaderView(filas);
    //POSICION DEL SCROLL
    Subirbajar.setCorner(JScrollPane.UPPER_LEFT_CORNER,filas.getTableHeader());

    ventana.add(Subirbajar);
}  
//FUNCION BARRA DE HERRAMIENTAS
public final void seet_Toolbar(){
    //VACIAR TEXTO AL HACER CLICK
    coord.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e){
            coord.setText("");
        }
    });
    impresion.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e){
            impresion.setText("");
        }
    });
    //BARRA ESTATICA
    barra.setFloatable(false);
    //PERSONALIZACION DE BARRA 
    barra.addSeparator();        barra.add(listaoper);
    barra.addSeparator();        barra.add(coord);
    barra.addSeparator();        barra.add(mostrar);
    barra.addSeparator();        barra.add(impresion);
    barra.addSeparator();        barra.add(envia);
    //AGREGAR BARRA CON UN BORDE AL INICIO
    ventana.add(barra,BorderLayout.PAGE_START);
} 
public final void geet_Informacion(){//Obtener informacion
    envia.addActionListener(new ox());
}  
public static void main(String[] args) {
Haxcell table = new Haxcell();
};
private class ox implements ActionListener{//Clase para manipular la informacion
    //int[] rowindices = tabla.getSelectedRows();
    //int[] colindices = tabla.getSelectedColumns();
    Object cosa;
    @Override
    public void actionPerformed(ActionEvent e) {
        cosa = tabla.getValueAt(tabla.getSelectedRow(),tabla.getSelectedColumn());
        //(rowindices,colindices);
        tabla.setValueAt(cosa, 8, 2);
    }
};}

How can I extract data in bulk (I'm using a free selection), and also how would I save that data, for later manipulation?

Thanks in advance. ;)

    
asked by Danny J. Parr 28.10.2017 в 01:54
source

1 answer

0

Step 1: Create a variable to manipulate the data in the table

 DefaultTableModel dtm = new DefaultTableModel(new Object [][]{},new String [] {
    "MODIFICAR", "SEMESTRE", "CODIGO", "ASIGNATURA", "UC", "Prelacion_1", "Prelacion_2", "Prelacion_3","Prelacion_4","Prelacion_5","Prelacion_6"
     }){
      Class[] types = new Class [] {
    java.lang.Boolean.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
        };

         public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
         }


     };

Step 2: Assign that structure to the table.

tabla.setModel(dtm);

Step 3: Add the interest data separated by a comma

dtm.addRow(informacion);
dtm.fireTableDataChanged();

optional: the information must be with the structure of the columns of the table, so it must be filled with all values so they are null

Object [] informacion ={new Boolean(false),null,null,null,null,null,null,null,null,null,null};
    
answered by 29.11.2017 в 03:04