Ver Mensaje Individual
  #4 (permalink)  
Antiguo 09/04/2007, 13:31
Avatar de Nighter
Nighter
 
Fecha de Ingreso: enero-2007
Mensajes: 97
Antigüedad: 19 años, 9 meses
Puntos: 0
Re: actualizar jTable??????

hola, antes que nada, por lo que veo estas haciendo la tabla, pero como una matriz y despues mandas esa info, yo no lo he probado asi, pero se me hace muy complicado la verdad, te recomiendo que leas un poco sobre modelos que son un objeto asociado a las tablas y puedes usar el DefaultTableModel o crear el tuyo propio implementando del AbstractTableModel, esto es muy util porque si le aplicas algun cambio al modelo, este se refleja de inmediato en la tabla (en la pantalla pues), te anexo un codigo de un proyecto que hice, espero que te sirva:

Código PHP:
package facturacion;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.postgresql.util.PSQLException;

public class 
GenerarFactura extends javax.swing.JFrame 
{
 private 
JFrame productosJF;
 private 
ResultSet rs, rs1, rs2;
 private 
String nombre;
 public static 
DefaultTableModel modelo; 
 public static 
double subtotal;
 private 
double iva, ivaPrecio, total;
 private 
n2t numero;
 private 
String id;
 private 
int renglones;
 private 
Vector detalles;

 public 
GenerarFactura() 
{
 
subtotal = 0.0;
 
modelo = new DefaultTableModel();
 
modelo.addColumn("ID PRODUCTO"); 
 
modelo.addColumn("CANTIDAD"); 
 
modelo.addColumn("DESCRIPCION"); 
 
modelo.addColumn("PRECIO UNITARIO"); 
 
modelo.addColumn("IMPORTE"); 

 
initComponents(); 
}

    
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    
private void initComponents() {
    
        
jTable1.setAutoCreateRowSorter(true);
        
jTable1.setModel(modelo);
        
jScrollPane1.setViewportView(jTable1);
    }
// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
modelo.removeRow(jTable1.getSelectedRow());
}
//GEN-LAST:event_jButton1ActionPerformed

private void registrarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registrarBtnActionPerformed
registrarBtn.setEnabled(false);

int i;

i = 0;

try
{
 
String fecha, query;
  
 
fecha = calendarioJCa.getSelectedYear();
 
fecha +="-"+ calendarioJCa.getSelectedMonth()+"-";
 
fecha += calendarioJCa.getSelectedDay(); 
 
 
query = "insert into speedy.factura values(DEFAULT,";
 
query += "'"+id+"','"+fecha+"',";
 
query += ivaTF.getText()+","+subtotalTF.getText()+",1)"; 
 
 
//System.out.println(query);

 
Servidor.conexion3.IUD(query);
 
renglones = modelo.getRowCount();
 
 while(
i<renglones)
 {
  
Servidor.conexion3.IUD("INSERT INTO speedy.facturadet VALUES("+noFacturaTF.getText()+",'"+modelo.getValueAt(i,0)+"',"+modelo.getValueAt(i,1)+","+modelo.getValueAt(i,4)+","+i+")");
  
//System.out.println("INSERT INTO speedy.facturadet VALUES("+noFacturaTF.getText()+",'"+modelo.getValueAt(i,0)+"',"+modelo.getValueAt(i,1)+","+modelo.getValueAt(i,4)+","+i+")");
  
i++;
 }
 
JOptionPane.showMessageDialog(this,"Se a registrado exitosamente la factura","Registro de factura",JOptionPane.INFORMATION_MESSAGE);
} 
catch(
PSQLException ex){JOptionPane.showMessageDialog(this,ex.getMessage(), "Error", JOptionPane.INFORMATION_MESSAGE);}
catch(
SQLException e){JOptionPane.showMessageDialog(this,e.getMessage(), "Error", JOptionPane.INFORMATION_MESSAGE);} 
}
//GEN-LAST:event_registrarBtnActionPerformed

private void siguienteReg()
{
 try
 {
  
rs = Servidor.conexion.selectQuery("select idfactura from speedy.factura");
  
rs.last();
  
noFacturaTF.setText(String.valueOf(Byte.parseByte(rs.getString(1))+1));
 }
 catch(
java.sql.SQLException e)
 {
  
JOptionPane.showMessageDialog(this, "No se pudo resolver la peticion, \nla base de datos no contiene informacion", "Error en peticion", JOptionPane.ERROR_MESSAGE);
  
noFacturaTF.setText("1");
 }
}

private 
void totalTFCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_totalTFCaretUpdate
numero = new n2t(); 
n2tTF.setText(numero.convertirLetras((int)total));
}
//GEN-LAST:event_totalTFCaretUpdate

private void ivaTFCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_ivaTFCaretUpdate
total = subtotal+ivaPrecio;
totalTF.setText(String.valueOf(total));
}
//GEN-LAST:event_ivaTFCaretUpdate

private void subtotalTFCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_subtotalTFCaretUpdate
ivaPrecio = iva*subtotal;
ivaTF.setText(String.valueOf(ivaPrecio));
}
//GEN-LAST:event_subtotalTFCaretUpdate

    
private void cancelarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBtnActionPerformed
this.dispose();
    }
//GEN-LAST:event_cancelarBtnActionPerformed

    
private void clientesCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clientesCBActionPerformed
try
{
 
rs2 = Servidor.conexion2.selectQuery("select idcliente, direccion, ciudad, rfc from speedy.cliente where cliente ='"+clientesCB.getSelectedItem().toString()+"'");
 
rs2.next();
 
 
id = rs2.getString(1);
 
dirTF.setText(rs2.getString(2));
 
ciudadTF.setText(rs2.getString(3));
 
rfcTF.setText(rs2.getString(4)); 
} 
catch(
PSQLException ex){JOptionPane.showMessageDialog(this,ex.getMessage(), "Error", JOptionPane.INFORMATION_MESSAGE);}
catch(
SQLException e){JOptionPane.showMessageDialog(this,e.getMessage(), "Error", JOptionPane.INFORMATION_MESSAGE);} 
    }
//GEN-LAST:event_clientesCBActionPerformed

    
private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
siguienteReg();
try
{
 
rs1 = Servidor.conexion1.selectQuery("select cliente from speedy.cliente");

 while(
rs1.next())
 {
  
clientesCB.addItem(rs1.getString(1));
 } 

 
rs1 = Servidor.conexion1.selectQuery("select * from speedy.iva");
 
rs1.next();
 
iva = Double.parseDouble(rs1.getString(1));
} 
catch(
PSQLException ex){JOptionPane.showMessageDialog(this,ex.getMessage(), "Error", JOptionPane.INFORMATION_MESSAGE);}
catch(
SQLException e){JOptionPane.showMessageDialog(this,e.getMessage(), "Error", JOptionPane.INFORMATION_MESSAGE);}   

registrarBtn.setEnabled(true);
    }
//GEN-LAST:event_formWindowOpened

private void selecionProductoPMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selecionProductoPMouseEntered
productosJF = new Productos();
//selecionProductoP.setEnabled(false);
productosJF.setVisible(true);
}
//GEN-LAST:event_selecionProductoPMouseEntered


}