import claseConectar.conectar;
import java.sql.*;
import java.util.logging.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Juancruz
*/
public class IngresoProductos extends javax.swing.JInternalFrame {
DefaultTableModel model;
/** Creates new form IngresoProductos */
public IngresoProductos() {
initComponents();
this.setLocation(150,15 );
bloquear();
cargar("");
}
void bloquear(){
txtcod.setEnabled(false);
txtdes.setEnabled(false);
txtpre.setEnabled(false);
txtstock.setEnabled(false);
btnguardar.setEnabled(false);
btnnuevo.setEnabled(true);
btncancelar.setEnabled(false);
btnactualizar.setEnabled(false);
}
void limpiar(){
txtcod.setText("");
txtdes.setText("");
txtpre.setText("");
txtstock.setText("");
}
void desbloquear(){
txtcod.setEnabled(true);
txtdes.setEnabled(true);
txtpre.setEnabled(true);
txtstock.setEnabled(true);
btnguardar.setEnabled(true);
btnnuevo.setEnabled(false);
btncancelar.setEnabled(true);
}
void cargar(String valor) {
try{
String [] titulos={"Codigo","Descripcion","Precio","Stock"};
String [] registros= new String[4];
model=new DefaultTableModel(null,titulos);
model.addRow(registros);
}
tbproductos.setModel(model);
tbproductos.getColumnModel().getColumn(0).setPreferredWidth(150);
tbproductos.getColumnModel().getColumn(1).setPreferredWidth(300);
tbproductos.getColumnModel().getColumn(2).setPreferredWidth(100);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
void BuscarProductoEditar(String cod) {
try{
String codi="",desc="",prec="",stock="";
String cons="select * from producto WHERE cod_pro='"+cod+"'";
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(cons);
while(rs.next())
{
codi=rs.getString(1);
desc=rs.getString(2);
prec=rs.getString(3);
stock=rs.getString(4);
}
txtcod.setText(codi);
txtdes.setText(desc);
txtpre.setText(prec);
txtstock.setText(stock);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void codigos(){
int j;
int cont=1;
String num="";
String c="";
String SQL="select max(cod_pro) from producto";
// String SQL="select count(*) from factura";
//String SQL="SELECT MAX(cod_emp) AS cod_emp FROM empleado";
//String SQL="SELECT @@identity AS ID";
try {
Statement st = cn.createStatement();
ResultSet rs=st.executeQuery(SQL);
if(rs.next())
{
c=rs.getString(1);
}
if(c==null){
txtcod.setText("CP0001");
}
else{
char r1=c.charAt(2);
char r2=c.charAt(3);
char r3=c.charAt(4);
char r4=c.charAt(5);
String r="";
r=""+r1+r2+r3+r4;
j=Integer.parseInt(r);
GenerarCodigos gen= new GenerarCodigos();
gen.generar(j);
txtcod.setText("CP"+gen.serie());
}
} catch (SQLException ex) {
Logger.getLogger(Factura.class.getName()).log(Level.SEVERE, null, ex);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTable1.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(jTable1);
mnactualizar.setText("Modificar");
mnactualizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnactualizarActionPerformed(evt);
}
});
jPopupMenu1.add(mnactualizar);
mneliminar.setText("Eliminar");
mneliminar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mneliminarActionPerformed(evt);
}
});
jPopupMenu1.add(mneliminar);
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
setTitle("REGISTRO DE PRODUCTOS");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalle de Producto"));
jLabel1.setText("Codigo:");
txtcod.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtcodActionPerformed(evt);
}
});
jLabel2.setText("Descripcion:");
txtdes.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtdesActionPerformed(evt);
}
});
jLabel3.setText("Precio:");
txtpre.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtpreActionPerformed(evt);
}
});
jLabel5.setText("Stock:");
txtstock.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtstockActionPerformed(evt);
}
});
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtstock, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtcod, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtpre, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtdes, javax.swing.GroupLayout.PREFERRED_SIZE, 97,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(22, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtcod, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(txtdes, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(txtpre, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(txtstock, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(25, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
btnnuevo.setText("Nuevo");
btnnuevo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnnuevoActionPerformed(evt);
}
});
btnguardar.setText("Grabar");
btnguardar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnguardarActionPerformed(evt);
}
});
btnactualizar.setText("Actualizar");
btnactualizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnactualizarActionPerformed(evt);
}
});
btncancelar.setText("Cancelar");
btncancelar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btncancelarActionPerformed(evt);
}
});
btnsalir.setText("Salir");
btnsalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsalirActionPerformed(evt);
}
});
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(btnactualizar, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnnuevo, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnguardar, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btncancelar, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnsalir, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnnuevo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnguardar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnactualizar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btncancelar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnsalir)
.addContainerGap(27, Short.MAX_VALUE))
);
tbproductos.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{},
{},
{},
{}
},
new String [] {
}
));
tbproductos.setComponentPopupMenu(jPopupMenu1);
jScrollPane2.setViewportView(tbproductos);
jLabel4.setText("Buscar:");
txtbuscar.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtbuscarKeyReleased(evt);
}
});
jButton1.setText("Mostrar Todo");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
pack();
}// </editor-fold>
try {
int filaMod=tbproductos.getSelectedRow();
if(filaMod==-1)
{
JOptionPane.showMessageDialog(null, "Seleccione alguna fila");
}
else
{
btnactualizar.setEnabled(true);
String cod=(String)tbproductos.getValueAt(filaMod, 0);
desbloquear();
BuscarProductoEditar(cod);
}
} catch (Exception e) {
}
}