Foro sobre Java SE > Llenado de JTable a traves de MySql y edicion doble click
Si aquí no encuentras la solución, plantea la cuestión de manera más concreta, y con código de ejemplo.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection
utilice una libreria llamada rs2xml.jar que es la que importa los DbUtils para mostrar los datos en la tabla, pero me gustaria poder especificar que datos quisiera mostrar y ejecutar una accion cuando le de doble click en una celda o fila de la tabla
saludos
package sicap;
import java.sql.*;
import net.proteanit.sql.DbUtils;
public class Busqueda extends javax.swing.JFrame {
public Busqueda() {
initComponents();
this.setLocationRelativeTo(null);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
campoNombre = new javax.swing.JTextField();
campoPaterno = new javax.swing.JTextField();
campoMaterno = new javax.swing.JTextField();
campoMatricula = new javax.swing.JTextField();
campoGeneracion = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tabla = new javax.swing.JTable();
jLabel7 = new javax.swing.JLabel();
campoPlantel = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Colegio de Bachilleres del Estado de Tlaxcala");
setAlwaysOnTop(true);
setResizable(false);
jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jLabel1.setFont(new java.awt.Font("Minion Pro", 1, 36)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Busqueda de Certificados");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 698, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(33, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabel1)
.addContainerGap(25, Short.MAX_VALUE))
);
jLabel2.setText("Nombre");
jLabel3.setText("Paterno");
jLabel4.setText("Materno");
jLabel5.setText("Matricula");
jLabel6.setText("Generación");
jButton1.setText("Buscar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
tabla.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tabla);
jLabel7.setText("Plantel");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jLabel2)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(campoPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(jLabel3)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jLabel4)
.addGap(86, 86, 86)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
.addComponent(jLabel7)
.addGap(47, 47, 47)
.addComponent(jLabel6)))
.addGap(15, 15, 15))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(campoMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(campoPlantel)
.addGap(28, 28, 28)
.addComponent(campoGeneracion, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(25, 25, 25)))))
.addGap(12, 12, 12))
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6)
.addComponent(jLabel7))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(campoPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(campoMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(campoGeneracion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(campoPlantel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(93, 93, 93)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(262, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/certificados","root","");
Statement estado = conn.createStatement();
ResultSet resultado = estado.executeQuery("select * from alumno where (nombre like \"%"+campoNombre.getText()+"%\") and (paterno like \"%"+campoPaterno.getText()+"%\")"
+ " and (materno like \"%"+campoMaterno.getText()+"%\") and (periodo like \"%"+campoGeneracion.getText()+"%\")and (plantel like \"%"+campoPlantel.getText()+"%\")and (matricula like \"%"+campoMatricula.getText()+"%\")");
while (resultado.next()){
// probando si muestra los datos en un JTextField
//campoResultado.setText(resultado.getString("nombre")+" "+resultado.getString("paterno")+" "+resultado.getString("materno")+" "+"Plantel "+resultado.getString("plantel")+" "+"Generacion "+resultado.getString("periodo")+" "+"Matricula "+resultado.getString("matricula")+" "+"Imagen "+resultado.getString("imagen"));
tabla.setVisible(true);
tabla.setModel(DbUtils.resultSetToTableModel(resultado));
}
}
catch (Exception er){
System.out.println("no conecta");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Busqueda().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField campoGeneracion;
private javax.swing.JTextField campoMaterno;
private javax.swing.JTextField campoMatricula;
private javax.swing.JTextField campoNombre;
private javax.swing.JTextField campoPaterno;
private javax.swing.JTextField campoPlantel;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tabla;
En ese enlace de más arriba tienes la solución, con ejemplos de uso.
Muy buenas noches o buenas segun el horario que tengan, mi duda es esta, yo ya tengo hechos mis formularios realizados en Swing, la conexion hecha a MySql y tanto las entradas y busquedas en la base de datos, pero me gustaria presentar los resultados de las busquedas en una tabla, ya lo hice con el RS2XML solo que lo hace con todos los campos y no me permite (o no encontrado como) que me muestre ciertos campos y en determinado orden, pero mi duda va mas alla:
lo que me gustaria que hiciera es que al encontrar los resultados le pueda dar doble click en un campo y de ahi hacer otra accion, supongamos de una lista de 10 nombres le doy doble click al numero 7 y de ahi pueda cargar un expediente, aqui como le podria hacer?
saludos y agradezco la atencion.