private void producto() { DefaultTableModel modelo = new DefaultTableModel(); try { Statement estatuto = conex.getCn().createStatement(); ResultSet resultado = estatuto.executeQuery( "SELECT idProducto Codigo, Producto, Precio, IF(Servicio = true, 'Servicio', Cantidad) Cantidad FROM producto order by Producto asc"); String[] Titulos = {"Codigo", "Nombre", "Precio", "Cantidad"}; modelo.setColumnIdentifiers(Titulos); while (resultado.next()) { Object[] fila = { resultado.getObject("Codigo"), resultado.getObject("Producto"), resultado.getObject("Precio"), resultado.getObject("Cantidad") }; modelo.addRow(fila); } TableProducto.setModel(modelo); } catch (SQLException e) { setTitle(e.toString()); JOptionPane.showMessageDialog( null, e.toString(), "Student Control", JOptionPane.INFORMATION_MESSAGE); } }
private void TableProductoMouseClicked( java.awt.event.MouseEvent evt) { // GEN-FIRST:event_TableProductoMouseClicked int filaselected = TableProducto.getSelectedRow(); String codigo = " ", producto = " ", precio = " ", cantidad = " "; try { if (filaselected == -1) { JOptionPane.showMessageDialog(null, "Debe Seleccionar un producto"); } else { TableProducto.getModel(); // codigo = TableProducto.getValueAt(filaselected, 0).toString(); producto = TableProducto.getValueAt(filaselected, 1).toString(); precio = TableProducto.getValueAt(filaselected, 2).toString(); cantidad = TableProducto.getValueAt(filaselected, 3).toString(); codigo = TableProducto.getValueAt(filaselected, 0).toString(); NombreP.setText(producto); PrecioP.setText(precio); CantidadP.setText(cantidad); cod.setText(codigo); } } catch (HeadlessException e) { } // TODO add your handling code here: } // GEN-LAST:event_TableProductoMouseClicked
/** * 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">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); CantidadP = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); TableProducto = new javax.swing.JTable(); jLabel1 = new javax.swing.JLabel(); NombreP = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); btnGuardar = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); PrecioP = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); Servicio = new javax.swing.JCheckBox(); jButton1 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); cod = new javax.swing.JLabel(); setTitle("Ingreso de Productos"); jPanel1.setBorder( new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); jPanel1.setOpaque(false); jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jPanel1.add(CantidadP, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 85, 202, 28)); TableProducto.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"})); TableProducto.addMouseListener( new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { TableProductoMouseClicked(evt); } }); jScrollPane1.setViewportView(TableProducto); jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(13, 140, 361, 208)); jLabel1.setForeground(new java.awt.Color(255, 255, 255)); jLabel1.setText("Producto:"); jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(13, 14, 155, 31)); NombreP.addKeyListener( new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { NombrePKeyReleased(evt); } }); jPanel1.add(NombreP, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 14, 202, 31)); jLabel2.setForeground(new java.awt.Color(255, 255, 255)); jLabel2.setText("Precio:"); jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(13, 51, 155, 28)); btnGuardar.setIcon( new javax.swing.ImageIcon(getClass().getResource("/Recursos/guardar.png"))); // NOI18N btnGuardar.setText("Guardar"); btnGuardar.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnGuardarActionPerformed(evt); } }); jPanel1.add(btnGuardar, new org.netbeans.lib.awtextra.AbsoluteConstraints(13, 354, -1, 61)); jButton2.setIcon( new javax.swing.ImageIcon(getClass().getResource("/Recursos/delete.png"))); // NOI18N jButton2.setText("Salir"); jButton2.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(293, 354, -1, 61)); jPanel1.add(PrecioP, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 51, 202, 28)); jLabel3.setForeground(new java.awt.Color(255, 255, 255)); jLabel3.setText("Cantidad:"); jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(13, 85, 155, 28)); Servicio.setText("Es Servicio?"); Servicio.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ServicioActionPerformed(evt); } }); jPanel1.add(Servicio, new org.netbeans.lib.awtextra.AbsoluteConstraints(13, 115, -1, -1)); jButton1.setText("jButton1"); jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(156, 140, -1, -1)); jButton3.setText("Actualizar"); jButton3.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(143, 360, 90, 50)); cod.setText("codigo"); cod.setEnabled(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addContainerGap() .addComponent( jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE) .addContainerGap()) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(cod) .addGap(105, 105, 105))); 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) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(cod) .addContainerGap())); pack(); } // </editor-fold>//GEN-END:initComponents