Hacer un Maestro/Detalle en un Formulario
Publicado por Mario (6 intervenciones) el 29/11/2019 00:25:58
Saludos, Este es el codigo que estoy usando para crear el Mestro Detalle, en el primer JTable coloco la tabla maestra atravez de una consulta y en el JTable de abajo Coloco otra consulta que seria todas las entradas realizadas por una visita a cierto lugar ... al mover ya sea con un click o mover la tecla hacia arriba o hacia abajo enviarle el idvisita a la consulta de abajo ... esa es mi idea pero no logro aterrizarla ... ayuda por favor ... de antemano muchas gracias ...
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
public void iniciartbl() {
DefaultTableModel model = new DefaultTableModel();
String sql = "select "
+ "v.idvisita, "
+ "concat_ws(' ', v.apellidos, v.nombre) AS Nombre, "
+ "est.nombre AS Estado, "
+ "mun.nombre AS Municipio, "
+ "loc.nombre AS Localidad, "
+ "v.direccion "
+ "from "
+ "visitas v "
+ "left join cat_localidad.estados est on v.idestado = est.id "
+ "left join cat_localidad.municipios mun on v.idmunicipio = mun.id "
+ "left join cat_localidad.localidades loc on v.idlocalidad = loc.id "
+ "where "
+ "v.idestado = est.id "
+ "order by "
+ "Nombre ASC";
try {
//java.sql.Connection cn = Conexion.conectar();
Connection cnloc = (Connection) Conexion_estados.conectar_estados();
PreparedStatement pst = cn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
tblvisitas = new JTable(model);
pnlvisitas.setViewportView(tblvisitas);
model.addColumn("ID");
model.addColumn("NOMBRE");
model.addColumn("ESTADO");
model.addColumn("MUNICIPIO");
model.addColumn("LOCALIDAD");
model.addColumn("DIRECCION");
while (rs.next()) {
Object[] fila = new Object[6];
for (int i = 0; i < 6; i++) {
fila[i] = rs.getObject(i + 1);
}
model.addRow(fila);
}
//Color Diferente al Encabezado ...
JTableHeader Theader = tblvisitas.getTableHeader();
Theader.setBackground(java.awt.Color.GREEN);
Theader.setForeground(java.awt.Color.RED);
((DefaultTableCellRenderer) Theader.getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
//Centrar Celdas ...
DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
cellRenderer.setHorizontalAlignment(SwingConstants.CENTER);
tblvisitas.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
//Activar ScrollBar
tblvisitas.setAutoResizeMode(tblvisitas.AUTO_RESIZE_OFF);
//Anchos de cada columna
int[] anchos = {35, 200, 130, 120, 120, 260};
for (int i = 0; i < tblvisitas.getColumnCount(); i++) {
tblvisitas.getColumnModel().getColumn(i).setPreferredWidth(anchos[i]);
}
//Colocar Alto de Filas
tblvisitas.setRowHeight(25);
//Cerramos Conexion ..
//cn.close();
cnloc.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error al Iniciar Master..." + e);
}
tblvisitas.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
DefaultTableModel detalle = new DefaultTableModel();
int fila_point = tblvisitas.rowAtPoint(e.getPoint());
int columna_point = 0;
JOptionPane.showMessageDialog(null, "el valor de fila_point es: " + fila_point);
if (fila_point > 0) {
id_visita = (int) detalle.getValueAt(fila_point, columna_point);
JOptionPane.showMessageDialog(null, "Valor de id_visita es: " + id_visita);
String sql1 = "select "
+ "vh.idvisita, "
+ "vh.numTarjeta, "
+ "vh.fechaEntrada, "
+ "vh.fechaSalida, "
+ "a.area, "
+ "vh.asunto, "
+ "ident.nombredoc, "
+ "vh.numidentificacion, "
+ "concat_ws(' ', empl.apellidos, empl.nombre) AS Referencia, "
+ "concat_ws(' ', empl1.apellidos, empl1.nombre) AS Responsable, "
+ "if(vh.carro = 1, 'Sin Carro', 'Con Carro') AS Vehiculo "
+ "from "
+ "visitas v "
+ "left join visitas_historial vh on vh.idvisita = v.idvisita "
+ "left join cat_areas a on vh.idarea = a.idarea "
+ "left join cat_identificacion ident on vh.identificacion = ident.identificacion "
+ "left join empleados empl on empl.idempl = vh.idreferencia "
+ "left join empleados empl1 on empl1.idempl = vh.idresponsable "
+ "where vh.idvisita = "
+ "'" + id_visita + "'";
try {
JOptionPane.showMessageDialog(null, "Entro a preparar la SQL");
//java.sql.Connection cn = Conexion.conectar();
PreparedStatement pst1 = cn.prepareStatement(sql1);
ResultSet rs1 = pst1.executeQuery();
tblhistorial = new JTable(detalle);
pnlhistorial.setViewportView(tblhistorial);
detalle.addColumn("ID");
detalle.addColumn("TARJ");
detalle.addColumn("ENTRADA");
detalle.addColumn("SALIDA");
detalle.addColumn("AREA VISITO");
detalle.addColumn("ASUNTO");
detalle.addColumn("IDENT");
detalle.addColumn("NUM");
detalle.addColumn("VEH");
while (rs1.next()) {
Object[] fila = new Object[9];
for (int i = 0; i < 9; i++) {
fila[i] = rs1.getObject(i + 1);
}
detalle.addRow(fila);
}
//Color Diferente al Encabezado ...
JTableHeader Theader = tblhistorial.getTableHeader();
Theader.setBackground(java.awt.Color.GREEN);
Theader.setForeground(java.awt.Color.RED);
((DefaultTableCellRenderer) Theader.getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
//Centrar Celdas ...
DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
cellRenderer.setHorizontalAlignment(SwingConstants.CENTER);
tblhistorial.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
//Activar ScrollBar
tblhistorial.setAutoResizeMode(tblhistorial.AUTO_RESIZE_OFF);
//Anchos de cada columna
int[] anchos = {35, 40, 80, 80, 120, 80, 80, 80, 50};
for (int i = 0; i < tblhistorial.getColumnCount(); i++) {
tblhistorial.getColumnModel().getColumn(i).setPreferredWidth(anchos[i]);
}
//Colocar Alto de Filas
tblhistorial.setRowHeight(25);
JOptionPane.showMessageDialog(null, "Estoy al Final...");
//Cerramos Conexion ..
//cn.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Error al Iniciar Detalles..." + ex);
}
}
}
});
}
Valora esta pregunta


0