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 java.util.ArrayList; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.JOptionPane; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; import primero.Ingenieros; import primero.RelProIng; import primero.RelProIngId; import primero.SessionFactoryUtil; /** * * @author Oier */ public class E4anadirIng extends javax.swing.JFrame { final static private SessionFactory sf = SessionFactoryUtil.getSessionFactory(); final static private Session sese = sf.openSession(); /** * Creates new form E4anadirIng */ private void cargarIngs() { sese.beginTransaction(); Criteria cri = sese.createCriteria(Ingenieros.class) .add(Restrictions.gtProperty("horasdisponible", "horasminimo")); List<Ingenieros> l = cri.list(); List<String> li = new ArrayList<String>(); li.add("Seleccione un Ingeniero:"); for (Object o : l) { Ingenieros i = (Ingenieros) o; li.add(i.getId() + ""); } DefaultComboBoxModel modelo = new DefaultComboBoxModel(li.toArray()); cbIng.setModel(modelo); sese.getTransaction().commit(); } public E4anadirIng() { initComponents(); cargarIngs(); } /** * 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() { cbIng = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); tfIngN = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); tfHoras = new javax.swing.JTextField(); bGuardar = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); cbIng.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); cbIng.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cbIngActionPerformed(evt); } }); jLabel1.setText("Ingeniero:"); tfIngN.setEnabled(false); jLabel2.setText("Horas:"); bGuardar.setText("Guardar"); bGuardar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bGuardarActionPerformed(evt); } }); 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) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(cbIng, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tfIngN).addComponent(tfHoras, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(bGuardar)) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cbIng, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tfIngN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(tfHoras, 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 cbIngActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbIngActionPerformed String s = (String) cbIng.getSelectedItem(); if (s.equals("Seleccione un Ingeniero:")) { tfIngN.setText(""); } else { int id = Integer.parseInt(s); Ingenieros i = (Ingenieros) sese.get(Ingenieros.class, id); tfIngN.setText(i.getNombre()); } }//GEN-LAST:event_cbIngActionPerformed private void bGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardarActionPerformed String s = (String) cbIng.getSelectedItem(); if (s.equals("Seleccione un Ingeniero:")) { JOptionPane.showMessageDialog(null, "Debe selecionar un Ingeniero", "Error", JOptionPane.ERROR_MESSAGE); } else { if (isNumeric(tfHoras.getText())) { sese.beginTransaction(); int idIng = Integer.parseInt(s); int horas = Integer.parseInt(tfHoras.getText()); Ingenieros i = (Ingenieros) sese.get(Ingenieros.class, idIng); if (horas <= i.getHorasdisponible()) { i.setHorasdisponible(i.getHorasdisponible() - horas); RelProIng rpi = new RelProIng(); rpi.setId(new RelProIngId(E4.pActual.getIdproyecto(), i.getId())); rpi.setHoras(horas); rpi.setProyectos(E4.pActual); rpi.setIngenieros(i); sese.update(i); sese.save(rpi); sese.getTransaction().commit(); this.dispose(); } } else { JOptionPane.showMessageDialog(null, "El campo nmero de horas debe ser un nmero.", "Error", JOptionPane.ERROR_MESSAGE); } } }//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(E4anadirIng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(E4anadirIng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(E4anadirIng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(E4anadirIng.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 E4anadirIng().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton bGuardar; private javax.swing.JComboBox cbIng; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField tfHoras; private javax.swing.JTextField tfIngN; // End of variables declaration//GEN-END:variables }