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 ejer3; import antlr.StringUtils; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; import javax.swing.DefaultComboBoxModel; import javax.swing.JFormattedTextField; import javax.swing.JOptionPane; import javax.swing.text.NumberFormatter; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Projections; import primero.Empresas; import primero.Proyectos; import primero.SessionFactoryUtil; /** * * @author Oier */ public class E4anadirNuevo extends javax.swing.JFrame { final static private SessionFactory sf = SessionFactoryUtil.getSessionFactory(); final static private Session sese = sf.openSession(); /** * Creates new form E4anadirNuevo */ private void cargarEmpresas() { try { sese.beginTransaction(); List<Object> l = sese.createCriteria(Empresas.class).list(); List<String> li = new ArrayList<String>(); li.add("Seleccione una Empresa:"); for (Object o : l) { Empresas e = (Empresas) o; li.add(e.getId() + ""); } DefaultComboBoxModel modelo = new DefaultComboBoxModel(li.toArray()); cbEmpresas.setModel(modelo); sese.getTransaction().commit(); } catch (RuntimeException e) { sese.getTransaction().rollback(); throw e; } } public E4anadirNuevo() { initComponents(); cargarEmpresas(); dcFecha.setDate(new Date()); } /** * 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() { cbEmpresas = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); tfHorasN = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); dcFecha = new com.toedter.calendar.JDateChooser(); bGuardar = new javax.swing.JButton(); tfNombreEmpresa = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); cbEmpresas.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); cbEmpresas.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cbEmpresasActionPerformed(evt); } }); jLabel1.setText("Empresa:"); tfHorasN.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tfHorasNActionPerformed(evt); } }); jLabel2.setText("Horas Necesarias:"); jLabel3.setText("Fecha"); bGuardar.setText("Aadir"); bGuardar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bGuardarActionPerformed(evt); } }); tfNombreEmpresa.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().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(tfHorasN) .addComponent(dcFecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(bGuardar)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3)) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(cbEmpresas, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(tfNombreEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addGap(7, 7, 7) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cbEmpresas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tfNombreEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(tfHorasN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(dcFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(bGuardar) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void tfHorasNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfHorasNActionPerformed // TODO add your handling code here: }//GEN-LAST:event_tfHorasNActionPerformed private void cbEmpresasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbEmpresasActionPerformed String s = (String) cbEmpresas.getSelectedItem(); if (s.equals("Seleccione una Empresa:")) { tfNombreEmpresa.setText(""); } else { int id = Integer.parseInt(s); Empresas e = (Empresas) sese.get(Empresas.class, id); tfNombreEmpresa.setText(e.getNombre()); } // TODO add your handling code here: }//GEN-LAST:event_cbEmpresasActionPerformed private void bGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardarActionPerformed String e = (String) cbEmpresas.getSelectedItem(); if (e.equals("Seleccione una Empresa:")) { JOptionPane.showMessageDialog(null, "Debe selecionar una Empresa", "Error", JOptionPane.ERROR_MESSAGE); } else { String hnS = tfHorasN.getText(); if (!isNumeric(hnS)) { JOptionPane.showMessageDialog(null, "El campo nmero de horas debe ser un nmero.", "Error", JOptionPane.ERROR_MESSAGE); } else { Criteria criteria = sese.createCriteria(Proyectos.class) .setProjection(Projections.max("idproyecto")); int idproyecto = (Integer) criteria.uniqueResult(); int hn = Integer.parseInt(hnS); sese.beginTransaction(); int ide = Integer.parseInt(e); Proyectos p = new Proyectos(); idproyecto++; p.setIdproyecto(idproyecto); p.setEmpresas((Empresas) sese.get(Empresas.class, ide)); p.setHorasnecesarias(hn); p.setFecha(dcFecha.getDate()); sese.save(p); sese.getTransaction().commit(); this.dispose(); } } }//GEN-LAST:event_bGuardarActionPerformed private static boolean isNumeric(String str) { try { int i = Integer.parseInt(str); } catch (NumberFormatException nfe) { return false; } return true; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(E4anadirNuevo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(E4anadirNuevo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(E4anadirNuevo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(E4anadirNuevo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new E4anadirNuevo().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton bGuardar; private javax.swing.JComboBox cbEmpresas; private com.toedter.calendar.JDateChooser dcFecha; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JTextField tfHorasN; private javax.swing.JTextField tfNombreEmpresa; // End of variables declaration//GEN-END:variables }