×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Sidney Castro
Added: Oct 7, 2018 11:23 PM
Views: 3459
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package gui;
  7.  
  8. import java.sql.Connection;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import javax.swing.JOptionPane;
  12. import modelo.Aluno;
  13. import modelo.sql.AlunoSQL;
  14.  
  15. /**
  16.  *
  17.  * @author Sidney
  18.  */
  19. public class AlunoGUI extends javax.swing.JFrame {
  20.  
  21.     /**
  22.      * Creates new form AlunoGUI
  23.      */
  24.     AlunoSQL alunoSQL;
  25.     Aluno al;
  26.  
  27.     public AlunoGUI(Connection con) {
  28.         initComponents();
  29.  
  30.         this.alunoSQL = new AlunoSQL(con);
  31.  
  32.     }
  33.  
  34.     public AlunoGUI() {
  35.         initComponents();
  36.     }
  37.  
  38.     /**
  39.      * This method is called from within the constructor to initialize the form.
  40.      * WARNING: Do NOT modify this code. The content of this method is always
  41.      * regenerated by the Form Editor.
  42.      */
  43.     @SuppressWarnings("unchecked")
  44.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  45.     private void initComponents() {
  46.  
  47.         label1 = new javax.swing.JLabel();
  48.         jLabel2 = new javax.swing.JLabel();
  49.         nome = new javax.swing.JTextField();
  50.         idAluno = new javax.swing.JTextField();
  51.         jScrollPane1 = new javax.swing.JScrollPane();
  52.         jList1 = new javax.swing.JList<>();
  53.         msg = new javax.swing.JLabel();
  54.         jButton1 = new javax.swing.JButton();
  55.  
  56.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  57.         addWindowListener(new java.awt.event.WindowAdapter() {
  58.             public void windowOpened(java.awt.event.WindowEvent evt) {
  59.                 formWindowOpened(evt);
  60.             }
  61.         });
  62.  
  63.         label1.setText("ID:");
  64.  
  65.         jLabel2.setText("Nome:");
  66.  
  67.         nome.addActionListener(new java.awt.event.ActionListener() {
  68.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  69.                 nomeActionPerformed(evt);
  70.             }
  71.         });
  72.  
  73.         idAluno.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  74.         idAluno.setText("1");
  75.         idAluno.addActionListener(new java.awt.event.ActionListener() {
  76.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  77.                 idAlunoActionPerformed(evt);
  78.             }
  79.         });
  80.  
  81.         jList1.addMouseListener(new java.awt.event.MouseAdapter() {
  82.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  83.                 jList1MouseClicked(evt);
  84.             }
  85.         });
  86.         jScrollPane1.setViewportView(jList1);
  87.  
  88.         jButton1.setText("Altera");
  89.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  90.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  91.                 jButton1ActionPerformed(evt);
  92.             }
  93.         });
  94.  
  95.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  96.         getContentPane().setLayout(layout);
  97.         layout.setHorizontalGroup(
  98.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  99.             .addGroup(layout.createSequentialGroup()
  100.                 .addGap(24, 24, 24)
  101.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  102.                     .addGroup(layout.createSequentialGroup()
  103.                         .addComponent(label1)
  104.                         .addGap(18, 18, 18)
  105.                         .addComponent(idAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  106.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  107.                         .addComponent(jButton1))
  108.                     .addGroup(layout.createSequentialGroup()
  109.                         .addComponent(jLabel2)
  110.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  111.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  112.                             .addComponent(msg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  113.                             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
  114.                             .addComponent(nome))))
  115.                 .addContainerGap(35, Short.MAX_VALUE))
  116.         );
  117.         layout.setVerticalGroup(
  118.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  119.             .addGroup(layout.createSequentialGroup()
  120.                 .addContainerGap()
  121.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  122.                     .addComponent(label1)
  123.                     .addComponent(idAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  124.                     .addComponent(jButton1))
  125.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  126.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  127.                     .addComponent(jLabel2)
  128.                     .addComponent(nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  129.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  130.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
  131.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
  132.                 .addComponent(msg))
  133.         );
  134.  
  135.         pack();
  136.     }// </editor-fold>                        
  137.  
  138.     private void idAlunoActionPerformed(java.awt.event.ActionEvent evt) {                                        
  139.         nome.setText(null);
  140.         al = alunoSQL.buscaAluno(Integer.parseInt(idAluno.getText()));
  141.         if (al == null) {
  142.             nome.setText("Aluno não encontrado");
  143.         } else {
  144.             nome.setText(al.getNome());
  145.         }
  146.     }                                      
  147.  
  148.     private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
  149.  
  150.     }                                
  151.  
  152.     private void nomeActionPerformed(java.awt.event.ActionEvent evt) {                                    
  153.  
  154.         jList1.setModel(alunoSQL.alunosBuscar(nome.getText()));
  155.         idAluno.setText(null);
  156.         nome.setText(null);
  157.     }                                    
  158.  
  159.     private void jList1MouseClicked(java.awt.event.MouseEvent evt) {                                    
  160.         if (evt.getClickCount() == 2) {
  161.             al = jList1.getSelectedValue();
  162.             idAluno.setText(Integer.toString(al.getIdAluno()));
  163.             nome.setText(al.getNome());
  164.  
  165.         }
  166.     }                                  
  167.  
  168.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  169.  
  170.         Object[] opcoes = {"sim", "não"};
  171.         Object resposta;
  172.         resposta = JOptionPane.showInputDialog(null,
  173.                 "Deseja fazer a Alteração?",
  174.                 "Finalização",
  175.                 JOptionPane.PLAIN_MESSAGE,
  176.                 null,
  177.                 opcoes,
  178.                 "não");
  179.  
  180.         if (!idAluno.getText().isEmpty() && resposta=="sim") {
  181.             alunoSQL.alteraNome(Integer.parseInt(idAluno.getText()), nome.getText());
  182.         }
  183.  
  184.     }                                        
  185.  
  186.     /**
  187.      * @param args the command line arguments
  188.      */
  189.     public static void main(String args[]) {
  190.         /* Set the Nimbus look and feel */
  191.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  192.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  193.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  194.          */
  195.         try {
  196.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  197.                 if ("Nimbus".equals(info.getName())) {
  198.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  199.                     break;
  200.  
  201.                 }
  202.             }
  203.         } catch (ClassNotFoundException ex) {
  204.             java.util.logging.Logger.getLogger(AlunoGUI.class
  205.                     .getName()).log(java.util.logging.Level.SEVERE, null, ex);
  206.  
  207.         } catch (InstantiationException ex) {
  208.             java.util.logging.Logger.getLogger(AlunoGUI.class
  209.                     .getName()).log(java.util.logging.Level.SEVERE, null, ex);
  210.  
  211.         } catch (IllegalAccessException ex) {
  212.             java.util.logging.Logger.getLogger(AlunoGUI.class
  213.                     .getName()).log(java.util.logging.Level.SEVERE, null, ex);
  214.  
  215.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  216.             java.util.logging.Logger.getLogger(AlunoGUI.class
  217.                     .getName()).log(java.util.logging.Level.SEVERE, null, ex);
  218.         }
  219.         //</editor-fold>
  220.  
  221.         /* Create and display the form */
  222.         java.awt.EventQueue.invokeLater(new Runnable() {
  223.             public void run() {
  224.                 new AlunoGUI().setVisible(true);
  225.             }
  226.         });
  227.     }
  228.  
  229.     // Variables declaration - do not modify                    
  230.     private javax.swing.JTextField idAluno;
  231.     private javax.swing.JButton jButton1;
  232.     private javax.swing.JLabel jLabel2;
  233.     private javax.swing.JList<Aluno> jList1;
  234.     private javax.swing.JScrollPane jScrollPane1;
  235.     private javax.swing.JLabel label1;
  236.     private javax.swing.JLabel msg;
  237.     private javax.swing.JTextField nome;
  238.     // End of variables declaration                  
  239. }
  240.