Java - Recorrer un jtable para ir marcando las filas

 
Vista:

Recorrer un jtable para ir marcando las filas

Publicado por jamartinez66 (1 intervención) el 06/05/2016 15:56:50
Hola, tengo un jTable con tres campos de una bbdd y una cuarta columna que es un boolean, para que el usuario pueda marcar las filas que desee y poder operar después con ellas (solo con las seleccionadas).
También hay un botón que me debería marcar todas las filas, pero no sé cómo hacer para marcarlas y que el jTable se actualice y se muestren todas marcadas.

Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alexis

Recorrer un jtable para ir marcando las filas

Publicado por Alexis (15 intervenciones) el 20/05/2016 22:52:38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Tablita.setModel(new javax.swing.table.DefaultTableModel(datos, cabecera) {
            Class[] tipos = tiposColumnas;
            Class[] filas = tipoFilas;
 
            @Override
            public Class getColumnClass(int columnIndex) {
                // Este método es invocado por el CellRenderer para saber que dibujar en la celda,
                // observen que estamos retornando la clase que definimos de antemano.
                return tipos[columnIndex];
            }
 
            public Class getRowCountClass(int getRowCount) {
                return filas[getRowCount];//To change body of generated methods, choose Tools | Templates.
            }
        });
 
 
 
 
        //<editor-fold defaultstate="collapsed" desc="Evento de seleccion de toda la columna">  
        this.Tablita.getTableHeader().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
 
                System.out.println(Bandera1);
                int col = Tablita.columnAtPoint(e.getPoint());
                String name = Tablita.getColumnName(col);
                System.out.println("Column index selected " + col + " " + name);
 
                if (col == 3) {
                    if (Bandera1) {
 
                        if (col > 0) {
                            for (int i = 0; i < Tablita.getRowCount(); i++) {
                                if (Tablita.getValueAt(i, col) != null) {
                                    Tablita.setValueAt(true, i, col);
                                }
                            }
                        }
                        Bandera1 = false;
                    } else {
                        if (col > 0) {
                            for (int i = 0; i < Tablita.getRowCount(); i++) {
                                if (Tablita.getValueAt(i, col) != null) {
                                    Tablita.setValueAt(false, i, col);
                                }
                            }
                        }
                        Bandera1 = true;
                    }
                }
                if (col == 4) {
                    if (Bandera2) {
 
                        if (col > 0) {
                            for (int i = 0; i < Tablita.getRowCount(); i++) {
                                if (Tablita.getValueAt(i, col) != null) {
                                    Tablita.setValueAt(true, i, col);
                                }
                            }
                        }
                        Bandera2 = false;
                    } else {
                        if (col > 0) {
                            for (int i = 0; i < Tablita.getRowCount(); i++) {
                                if (Tablita.getValueAt(i, col) != null) {
                                    Tablita.setValueAt(false, i, col);
                                }
                            }
                        }
                        Bandera2 = true;
                    }
                }
                if (col == 5) {
                    if (Bandera3) {
 
                        if (col > 0) {
                            for (int i = 0; i < Tablita.getRowCount(); i++) {
                                if (Tablita.getValueAt(i, col) != null) {
                                    Tablita.setValueAt(true, i, col);
                                }
                            }
                        }
                        Bandera3 = false;
                    } else {
                        if (col > 0) {
                            for (int i = 0; i < Tablita.getRowCount(); i++) {
                                if (Tablita.getValueAt(i, col) != null) {
                                    Tablita.setValueAt(false, i, col);
                                }
                            }
                        }
                        Bandera3 = true;
                    }
                }
                if (col == 6) {
                    if (Bandera4) {
 
                        if (col > 0) {
                            for (int i = 0; i < Tablita.getRowCount(); i++) {
                                if (Tablita.getValueAt(i, col) != null) {
                                    Tablita.setValueAt(true, i, col);
                                }
                            }
                        }
                        Bandera4 = false;
                    } else {
                        if (col > 0) {
                            for (int i = 0; i < Tablita.getRowCount(); i++) {
                                if (Tablita.getValueAt(i, col) != null) {
                                    Tablita.setValueAt(false, i, col);
                                }
                            }
                        }
                        Bandera4 = true;
                    }
                }
            }
        });
        //</editor-fold>


en este ejemplo son mas de una columna booleana pero tu lo puedes ajustar con lo que necesites
y con esto lleno mi jtable desde la bd:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (int i = 0; i < listPMA.size(); i++) {
                Object[] filaTemp = new Object[7];
                PerfilModuloAccion tempObject = (PerfilModuloAccion) listPMA.get(i);
                filaTemp[0] = tempObject.getId();
                filaTemp[1] = tempObject.getIdModulo();
                for (int j = 0; j < listCMod.size(); j++) {
                    Modulos Modulo = (Modulos) listCMod.get(j);
                    if (tempObject.getIdModulo() == Modulo.getId()) {
                        filaTemp[2] = Modulo.getStrValor();
                    }
                }
 
                filaTemp[3] = tempObject.isAgregar();
                filaTemp[4] = tempObject.isEditar();
                filaTemp[5] = tempObject.isEliminar();
                filaTemp[6] = tempObject.isImprimir();
                this.TMod = (DefaultTableModel) this.Tablita.getModel();
                this.TMod.addRow(filaTemp);
                this.Tablita.setModel(TMod);
            }






ESPERO TE FUNCIONE SALUDOS
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar