Ver Mensaje Individual
  #3 (permalink)  
Antiguo 20/01/2015, 19:10
Avatar de soy_nicanor
soy_nicanor
 
Fecha de Ingreso: mayo-2010
Mensajes: 374
Antigüedad: 14 años
Puntos: 3
Respuesta: Cómo seleccionar una fila de un JTable de Java

Lo estoy desarrollando en Netbeans
El código lo tengo en el botón Buscar:
No me funciona


Código Java:
Ver original
  1. package borrar3;
  2. import javax.swing.table.DefaultTableModel;
  3. import BD.Conexion;
  4. import java.awt.event.KeyAdapter;
  5. import java.awt.event.KeyEvent;
  6. import java.sql.*;
  7. import javax.swing.JOptionPane;
  8.  
  9. public class Formulario extends javax.swing.JFrame {
  10.     DefaultTableModel modelo;
  11.  
  12.     /** Creates new form Formulario */
  13.     public Formulario() {
  14.         initComponents();
  15.        
  16.     }


Código Java:
Ver original
  1. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  2.        
  3.         String valor = jTextField1.getText();
  4.  
  5.         String[] titulos = {"ID", "Codigo", "Articulo", "cantidad", "precio S/:"};
  6.         String[] registro = new String[5];
  7.         String sSQL = "";
  8.         modelo = new DefaultTableModel(null, titulos);
  9.  
  10.         Conexion mysql = new Conexion();
  11.         Connection cn = mysql.Conectar();
  12.  
  13.         sSQL = "SELECT id, codigo, articulo, cantidad, precio FROM articulos " +
  14.                 "WHERE CONCAT(codigo, ' ',articulo) LIKE '%"+valor+"%'";
  15.         try
  16.         {
  17.             Statement st = cn.createStatement();
  18.             ResultSet rs = st.executeQuery(sSQL);
  19.  
  20.             while(rs.next())
  21.             {
  22.                 registro[0] = rs.getString("id");
  23.                 registro[1] = rs.getString("codigo");
  24.                 registro[2] = rs.getString("articulo");
  25.                 registro[3] = rs.getString("cantidad");
  26.                 registro[4] = rs.getString("precio");
  27.                 modelo.addRow(registro);
  28.             }
  29.  
  30.             for (int i = 0; i < jTable1.getRowCount(); i++)
  31.             {
  32.                 if (jTable1.getValueAt(i, 1).equals(valor))
  33.                 {
  34.                     jTable1.changeSelection(i, 1, false, false);
  35.                     break;
  36.                 }
  37.             }
  38.            
  39.  
  40.         }
  41.         catch (SQLException ex)
  42.         {
  43.             JOptionPane.showMessageDialog(null, ex);
  44.         }
  45.        
  46.     }
__________________
Muy Feliz