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 Agente; import Hibernate.Util.HibernateUtil; import Hibernate.entidades.Agente; import Hibernate.entidades.Usuario; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import Integral.Herramientas; /** * * @author ESPECIALIZADO TOLUCA */ public class altaAgente extends javax.swing.JDialog { public static final Agente RET_CANCEL = null; DefaultTableModel model; private Session session; Usuario user; String sessionPrograma; Herramientas h; /** * Creates new form altaCiclo */ public altaAgente(java.awt.Frame parent, boolean modal, Usuario u, String ses) { super(parent, modal); user = u; sessionPrograma = ses; initComponents(); } /** * 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(); jLabel9 = new javax.swing.JLabel(); t_descripcion = new javax.swing.JTextField(); b_cancelar = new javax.swing.JButton(); b_guardar = new javax.swing.JButton(); jLabel10 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Alta Agente"); setResizable(false); jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jPanel1.setPreferredSize(new java.awt.Dimension(461, 162)); jLabel9.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N jLabel9.setText("Agente:"); 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.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); 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("Guardar"); b_guardar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); b_guardar.setNextFocusableComponent(b_cancelar); b_guardar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b_guardarActionPerformed(evt); } }); 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().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addGap(0, 200, Short.MAX_VALUE) .addComponent(b_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(b_guardar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(13, 13, 13)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel10) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel9).addGap(24, 24, 24) .addComponent(t_descripcion).addContainerGap())))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(t_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(37, 37, 37) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(b_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b_guardar, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(8, 8, 8) .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, 12, Short.MAX_VALUE) .addContainerGap())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents 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 int opt = JOptionPane.showConfirmDialog(this, "Los datos capturados se eliminarn!"); System.out.println(opt); if (opt == 0) borra_cajas(); }//GEN-LAST:event_b_cancelarActionPerformed private void b_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_guardarActionPerformed b_cancelar.requestFocus(); h = new Herramientas(user, 0); h.session(sessionPrograma); if (t_descripcion.getText().compareTo("") != 0) { try { session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); if (consultaAgente(t_descripcion.getText()) == false) { Agente ajuste = new Agente(); ajuste.setNombre(t_descripcion.getText()); Integer ID = (Integer) session.save(ajuste); session.getTransaction().commit(); returnStatus = ajuste; if (ID != null) { JOptionPane.showMessageDialog(null, "Registro almacenado"); borra_cajas(); } } else { session.getTransaction().rollback(); JOptionPane.showMessageDialog(null, "El Agente ya existe!"); borra_cajas(); } } catch (HibernateException he) { he.printStackTrace(); session.getTransaction().rollback(); JOptionPane.showMessageDialog(null, "Error al guardar"); } finally { session.close(); } } else { JOptionPane.showMessageDialog(null, "Debe introducir el Agente!"); } }//GEN-LAST:event_b_guardarActionPerformed /** * @param args the command line arguments */ // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton b_cancelar; private javax.swing.JButton b_guardar; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; public javax.swing.JTextField t_descripcion; // End of variables declaration//GEN-END:variables 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 void borra_cajas() { t_descripcion.setText(""); } private boolean consultaAgente(String agente) { List<Object[]> resultList = executeHQLQuery("from Agente obj where obj.nombre='" + agente + "'"); if (resultList.size() > 0) return true; else return false; } private Agente returnStatus = RET_CANCEL; }