Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Ajustador; import Hibernate.Util.HibernateUtil; import Hibernate.entidades.Ajustador; import Hibernate.entidades.Usuario; import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import java.util.List; import java.util.Vector; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import Integral.Herramientas; import Integral.Render1; /** * * @author ESPECIALIZADO TOLUCA */ public class editaAjustador extends javax.swing.JPanel { //private Session session; DefaultTableModel model; Usuario usr; String sessionPrograma = ""; Herramientas h; private Ajustador actor1; String ic = ""; String[] columnas = new String[] { "Clave", "Ajustador" }; /** * Creates new form EditaCiclo */ public editaAjustador(Usuario usuario, String ses) { initComponents(); sessionPrograma = ses; usr = usuario; formatoTabla(); buscaDato(); } DefaultTableModel ModeloTablaReporte(int renglones, String columnas[]) { model = new DefaultTableModel(new Object[renglones][2], columnas) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class }; boolean[] canEdit = new boolean[] { false, false }; public void setValueAt(Object value, int row, int col) { Vector vector = (Vector) this.dataVector.elementAt(row); Object celda = ((Vector) this.dataVector.elementAt(row)).elementAt(col); switch (col) { case 0: vector.setElementAt(value, col); this.dataVector.setElementAt(vector, row); fireTableCellUpdated(row, col); break; default: vector.setElementAt(value, col); this.dataVector.setElementAt(vector, row); fireTableCellUpdated(row, col); break; } } public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }; return model; } /** * 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() { jPanel2 = new javax.swing.JPanel(); jPanel1 = new javax.swing.JPanel(); jPanel3 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); t_datos = new javax.swing.JTable(); Selecciona2 = new javax.swing.JButton(); Eliminar1 = new javax.swing.JButton(); Selecciona1 = new javax.swing.JButton(); bt_actualiza1 = new javax.swing.JButton(); t_busca = new javax.swing.JTextField(); b_busca = new javax.swing.JButton(); jPanel4 = new javax.swing.JPanel(); jLabel8 = new javax.swing.JLabel(); t_ajustador = new javax.swing.JTextField(); jLabel9 = new javax.swing.JLabel(); t_descripcion = new javax.swing.JTextField(); b_cancelar = new javax.swing.JButton(); b_guardar = new javax.swing.JButton(); setBackground(new java.awt.Color(254, 254, 254)); jPanel2.setBackground(new java.awt.Color(254, 254, 254)); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder( new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Edita Ajustador", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 12))); // NOI18N jPanel3.setBackground(new java.awt.Color(255, 255, 255)); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder( new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Lista", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 11))); // NOI18N jScrollPane1.setBackground(new java.awt.Color(254, 254, 254)); t_datos.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Clave", "Ajustador" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.Object.class }; boolean[] canEdit = new boolean[] { false, true }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); t_datos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); t_datos.getTableHeader().setReorderingAllowed(false); jScrollPane1.setViewportView(t_datos); Selecciona2.setBackground(new java.awt.Color(2, 135, 242)); Selecciona2.setForeground(new java.awt.Color(254, 254, 254)); Selecciona2.setIcon(new ImageIcon("imagenes/add-user.png")); Selecciona2.setText("Nuevo"); Selecciona2.setToolTipText("Agregar un registo actual"); Selecciona2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); Selecciona2.setMaximumSize(new java.awt.Dimension(87, 23)); Selecciona2.setMinimumSize(new java.awt.Dimension(87, 23)); Selecciona2.setNextFocusableComponent(b_cancelar); Selecciona2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Selecciona2ActionPerformed(evt); } }); Eliminar1.setBackground(new java.awt.Color(2, 135, 242)); Eliminar1.setForeground(new java.awt.Color(254, 254, 254)); Eliminar1.setIcon(new ImageIcon("imagenes/del-user.png")); Eliminar1.setText("Eliminar"); Eliminar1.setToolTipText("Eliminar el registro actual"); Eliminar1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); Eliminar1.setMaximumSize(new java.awt.Dimension(87, 23)); Eliminar1.setMinimumSize(new java.awt.Dimension(87, 23)); Eliminar1.setNextFocusableComponent(Selecciona2); Eliminar1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Eliminar1ActionPerformed(evt); } }); Selecciona1.setBackground(new java.awt.Color(2, 135, 242)); Selecciona1.setForeground(new java.awt.Color(254, 254, 254)); Selecciona1.setIcon(new ImageIcon("imagenes/update-user.png")); Selecciona1.setText("Seleccionar"); Selecciona1.setToolTipText("Seleccionar un registro de la tabla para editar"); Selecciona1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); Selecciona1.setNextFocusableComponent(Eliminar1); Selecciona1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Selecciona1ActionPerformed(evt); } }); bt_actualiza1.setBackground(new java.awt.Color(2, 135, 242)); bt_actualiza1.setForeground(new java.awt.Color(254, 254, 254)); bt_actualiza1.setIcon(new ImageIcon("imagenes/tabla.png")); bt_actualiza1.setText("Actualizar"); bt_actualiza1.setToolTipText("Actualizar los datos de la tabla"); bt_actualiza1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); bt_actualiza1.setMaximumSize(new java.awt.Dimension(87, 23)); bt_actualiza1.setMinimumSize(new java.awt.Dimension(87, 23)); bt_actualiza1.setNextFocusableComponent(Selecciona1); bt_actualiza1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bt_actualiza1ActionPerformed(evt); } }); t_busca.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { t_buscaActionPerformed(evt); } }); t_busca.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { t_buscaKeyTyped(evt); } }); b_busca.setIcon(new ImageIcon("imagenes/buscar1.png")); b_busca.setToolTipText("Busca una partida"); b_busca.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b_buscaActionPerformed(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup().addGap(0, 106, Short.MAX_VALUE) .addComponent(bt_actualiza1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(Selecciona1).addGap(18, 18, 18) .addComponent(Eliminar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(Selecciona2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel3Layout.createSequentialGroup().addComponent(t_busca) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(b_busca, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)) .addContainerGap())); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(t_busca).addComponent(b_busca, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Selecciona2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Eliminar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Selecciona1).addComponent(bt_actualiza1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap())); jPanel4.setBackground(new java.awt.Color(254, 254, 254)); jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder( new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Actualizar", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 11))); // NOI18N jLabel8.setText("Clave:"); t_ajustador.setNextFocusableComponent(t_descripcion); t_ajustador.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { t_ajustadorActionPerformed(evt); } }); t_ajustador.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { t_ajustadorKeyTyped(evt); } }); jLabel9.setText("Ajustador:"); t_descripcion.setNextFocusableComponent(b_guardar); t_descripcion.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { t_descripcionActionPerformed(evt); } }); t_descripcion.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { t_descripcionKeyTyped(evt); } }); b_cancelar.setBackground(new java.awt.Color(2, 135, 242)); b_cancelar.setForeground(new java.awt.Color(254, 254, 254)); b_cancelar.setIcon(new ImageIcon("imagenes/cancelar.png")); b_cancelar.setText("Cancelar"); b_cancelar.setEnabled(false); b_cancelar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); b_cancelar.setMaximumSize(new java.awt.Dimension(87, 23)); b_cancelar.setMinimumSize(new java.awt.Dimension(87, 23)); b_cancelar.setNextFocusableComponent(t_ajustador); b_cancelar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b_cancelarActionPerformed(evt); } }); b_guardar.setBackground(new java.awt.Color(2, 135, 242)); b_guardar.setForeground(new java.awt.Color(254, 254, 254)); b_guardar.setIcon(new ImageIcon("imagenes/guardar.png")); b_guardar.setText("Actualizar"); b_guardar.setEnabled(false); b_guardar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); b_guardar.setMaximumSize(new java.awt.Dimension(87, 23)); b_guardar.setMinimumSize(new java.awt.Dimension(87, 23)); b_guardar.setNextFocusableComponent(bt_actualiza1); b_guardar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b_guardarActionPerformed(evt); } }); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(b_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(b_guardar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(jPanel4Layout.createSequentialGroup().addGap(32, 32, 32) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel8).addComponent(jLabel9)) .addGap(18, 18, 18) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(t_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(t_ajustador, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(24, Short.MAX_VALUE))); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addGap(127, 127, 127) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(t_ajustador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(38, 38, 38) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(t_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(134, 134, 134) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(b_guardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(b_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(29, 29, 29))); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(44, 44, 44) .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(32, 32, 32))); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 996, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 501, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)))); }// </editor-fold>//GEN-END:initComponents private void Selecciona2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Selecciona2ActionPerformed b_cancelar.requestFocus(); h = new Herramientas(usr, 0); h.session(sessionPrograma); Session session = HibernateUtil.getSessionFactory().openSession(); usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario()); // if(usr.getEditarCiclo()==true) // { altaAjustador obj = new altaAjustador(new javax.swing.JFrame(), true, usr, sessionPrograma); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); obj.setLocation((d.width / 2) - (obj.getWidth() / 2), (d.height / 2) - (obj.getHeight() / 2)); obj.setVisible(true); borra_cajas(); cajas(false, false, false, false); buscaDato(); // } // else // JOptionPane.showMessageDialog(null, "Acceso denegado!"); if (session != null) if (session.isOpen()) session.close(); }//GEN-LAST:event_Selecciona2ActionPerformed private void Eliminar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Eliminar1ActionPerformed Selecciona2.requestFocus(); h = new Herramientas(usr, 0); h.session(sessionPrograma); Session session = HibernateUtil.getSessionFactory().openSession(); usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario()); /*if(usr.getEditaCiclo()==true) {*/ if (this.t_datos.getSelectedRow() >= 0) { DefaultTableModel model = (DefaultTableModel) t_datos.getModel(); int a = t_datos.getSelectedRow(); int opt = JOptionPane.showConfirmDialog(this, "Los datos capturados se eliminarn!"); if (JOptionPane.YES_OPTION == opt) { boolean respuesta = eliminar(t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString()); if (respuesta == true) { JOptionPane.showMessageDialog(null, "El Ajustador ha sido eliminado"); model.removeRow(a); borra_cajas(); cajas(false, false, false, false); buscaDato(); } } } else cajas(false, false, false, false); /*} else JOptionPane.showMessageDialog(null, "Acceso denegado");*/ if (session != null) if (session.isOpen()) session.close(); }//GEN-LAST:event_Eliminar1ActionPerformed private void Selecciona1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Selecciona1ActionPerformed Eliminar1.requestFocus(); h = new Herramientas(usr, 0); h.session(sessionPrograma); Session session = HibernateUtil.getSessionFactory().openSession(); usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario()); // if(usr!=null) // { if (t_datos.getSelectedRow() >= 0) { this.borra_cajas(); t_ajustador.setText(t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString()); ic = t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString(); t_descripcion.setText(t_datos.getValueAt(t_datos.getSelectedRow(), 1).toString()); this.cajas(true, true, true, true); } else JOptionPane.showMessageDialog(null, "No hay un Ajuste seleccionado!"); // } // else // JOptionPane.showMessageDialog(null, "Acceso denegado!"); if (session != null) if (session.isOpen()) session.close(); }//GEN-LAST:event_Selecciona1ActionPerformed private void bt_actualiza1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_actualiza1ActionPerformed Selecciona1.requestFocus(); borra_cajas(); cajas(false, false, false, false); buscaDato(); }//GEN-LAST:event_bt_actualiza1ActionPerformed private void t_ajustadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t_ajustadorActionPerformed t_descripcion.requestFocus(); }//GEN-LAST:event_t_ajustadorActionPerformed private void t_ajustadorKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_t_ajustadorKeyTyped char car = evt.getKeyChar(); if (this.t_ajustador.getText().length() >= 2) evt.consume(); if ((car < '0' || car > '9')) evt.consume(); }//GEN-LAST:event_t_ajustadorKeyTyped private void t_descripcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t_descripcionActionPerformed b_guardar.requestFocus(); }//GEN-LAST:event_t_descripcionActionPerformed private void t_descripcionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_t_descripcionKeyTyped evt.setKeyChar(Character.toUpperCase(evt.getKeyChar())); char car = evt.getKeyChar(); if (t_descripcion.getText().length() >= 200) evt.consume(); }//GEN-LAST:event_t_descripcionKeyTyped private void b_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_cancelarActionPerformed t_ajustador.requestFocus(); int opt = JOptionPane.showConfirmDialog(this, "Los datos capturados se eliminarn!"); if (opt == 0) { borra_cajas(); cajas(false, false, false, false); t_ajustador.requestFocus(); } }//GEN-LAST:event_b_cancelarActionPerformed private void b_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_guardarActionPerformed h = new Herramientas(usr, 0); h.session(sessionPrograma); bt_actualiza1.requestFocus(); Session session = HibernateUtil.getSessionFactory().openSession(); usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario()); // if(usr!=null) // { if (t_ajustador.getText().compareTo("") != 0) { boolean respuesta = modifica(); if (respuesta == true) { this.borra_cajas(); cajas(false, false, false, false); buscaDato(); } } else { JOptionPane.showMessageDialog(null, "Debe introducir el Ajustador que desees modificar!"); t_ajustador.requestFocus(); } // } // else // JOptionPane.showMessageDialog(null, "Acceso denegado!"); if (session != null) if (session.isOpen()) session.close(); }//GEN-LAST:event_b_guardarActionPerformed int x = 0; private void t_buscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t_buscaActionPerformed // TODO add your handling code here: if (this.t_busca.getText().compareToIgnoreCase("") != 0) { //buscaCuentas(); if (x >= t_datos.getRowCount()) { x = 0; java.awt.Rectangle r = t_datos.getCellRect(x, 1, true); t_datos.scrollRectToVisible(r); } for (; x < t_datos.getRowCount(); x++) { if (t_datos.getValueAt(x, 1).toString().indexOf(t_busca.getText().toUpperCase()) != -1) { t_datos.setRowSelectionInterval(x, x); t_datos.setColumnSelectionInterval(1, 1); java.awt.Rectangle r = t_datos.getCellRect(x, 1, true); t_datos.scrollRectToVisible(r); break; } } x++; } }//GEN-LAST:event_t_buscaActionPerformed private void b_buscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_buscaActionPerformed // TODO add your handling code here: this.t_buscaActionPerformed(null); }//GEN-LAST:event_b_buscaActionPerformed private void t_buscaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_t_buscaKeyTyped // TODO add your handling code here: }//GEN-LAST:event_t_buscaKeyTyped // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton Eliminar1; private javax.swing.JButton Selecciona1; private javax.swing.JButton Selecciona2; private javax.swing.JButton b_busca; private javax.swing.JButton b_cancelar; private javax.swing.JButton b_guardar; private javax.swing.JButton bt_actualiza1; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; public javax.swing.JPanel jPanel4; private javax.swing.JScrollPane jScrollPane1; public javax.swing.JTextField t_ajustador; private javax.swing.JTextField t_busca; private javax.swing.JTable t_datos; public javax.swing.JTextField t_descripcion; // End of variables declaration//GEN-END:variables public void borra_cajas() { this.t_ajustador.setText(""); this.t_descripcion.setText(""); } public void cajas(boolean ajustador, boolean descripcion, boolean cancelar, boolean guardar) { this.t_ajustador.setEnabled(ajustador); this.t_descripcion.setEnabled(descripcion); this.b_cancelar.setEnabled(cancelar); this.b_guardar.setEnabled(guardar); } private List<Object[]> executeHQLQuery(String hql) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.beginTransaction(); Query q = session.createQuery(hql); List resultList = q.list(); session.getTransaction().commit(); session.disconnect(); return resultList; } catch (HibernateException he) { he.printStackTrace(); List lista = null; return lista; } } private boolean eliminar(String idAjustador) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.beginTransaction(); actor1 = (Ajustador) session.get(Ajustador.class, Integer.parseInt(idAjustador)); if (actor1.getOrdens().isEmpty() == false) { session.getTransaction().rollback(); JOptionPane.showMessageDialog(null, "El Ajustador esta en uso en una orden no se puede eliminar!"); return false; } else { session.delete(actor1); session.getTransaction().commit(); return true; } } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); return false; } finally { if (session.isOpen()) session.close(); } } private boolean modifica() { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.beginTransaction(); ic = t_ajustador.getText(); Object resp = session.createQuery("from Ajustador obj where obj.idAjustador='" + t_ajustador.getText() + "' and obj.idAjustador!='" + ic + "'").uniqueResult(); actor1 = (Ajustador) session.get(Ajustador.class, (Integer.parseInt(t_ajustador.getText()))); if (resp == null) { Query q = session.createQuery("update Ajustador obj set obj.nombreAjustador='" + t_descripcion.getText() + "', obj.idAjustador='" + t_ajustador.getText() + "'where obj.idAjustador='" + ic + "'"); q.executeUpdate(); session.getTransaction().commit(); JOptionPane.showMessageDialog(null, "Registro modificado"); return true; } else { JOptionPane.showMessageDialog(null, "No se pueden guardar claves duplicadas"); return false; } //} } catch (HibernateException he) { he.printStackTrace(); System.out.println(he.hashCode()); session.getTransaction().rollback(); return false; } finally { if (session.isOpen()) session.close(); } } private void buscaDato() { String consulta = "from Ajustador"; List<Object[]> resultList = executeHQLQuery(consulta); if (resultList.size() > 0) { t_datos.setModel(ModeloTablaReporte(resultList.size(), columnas)); int i = 0; for (Object o : resultList) { actor1 = (Ajustador) o; model.setValueAt(actor1.getIdAjustador(), i, 0); model.setValueAt(actor1.getNombreAjustador(), i, 1); i++; } } else t_datos.setModel(ModeloTablaReporte(0, columnas)); formatoTabla(); } public void formatoTabla() { Color c1 = new java.awt.Color(2, 135, 242); for(int x=0; x<t_datos.getColumnModel().getColumnCount(); x++) t_datos.getColumnModel().getColumn(x).setHeaderRenderer(new Render1(c1)); tabla_tamaos(); t_datos.setShowVerticalLines(true); t_datos.setShowHorizontalLines(true); } public void tabla_tamaos() { TableColumnModel col_model = t_datos.getColumnModel(); for (int i=0; i<t_datos.getColumnCount(); i++) { TableColumn column = col_model.getColumn(i); switch(i) { case 0: column.setPreferredWidth(10); break; case 1: column.setPreferredWidth(150); break; default: column.setPreferredWidth(40); break; } } JTableHeader header = t_datos.getTableHeader(); header.setForeground(Color.white); } }