Advertisement
MarceloSousa

Untitled

May 23rd, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.79 KB | None | 0 0
  1. package frames;
  2.  
  3. import banco.Connect;
  4. import banco.ModeloTabela;
  5. import java.sql.Connection;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.ListSelectionModel;
  13.  
  14.  
  15. public class FormAlterar extends javax.swing.JFrame {
  16.  
  17.    Connection conexao = null;
  18.     public FormAlterar() {
  19.         initComponents();
  20.         try {
  21.             Connect.conectar();
  22.         } catch (ClassNotFoundException ex) {
  23.             JOptionPane.showMessageDialog(null, "Erro ao conectar!\n Erro:" +ex);
  24.         }
  25.         preencherTabela( );
  26.     }
  27.    
  28.  
  29.     /**
  30.      * This method is called from within the constructor to initialize the form.
  31.      * WARNING: Do NOT modify this code. The content of this method is always
  32.      * regenerated by the Form Editor.
  33.      */
  34.     @SuppressWarnings("unchecked")
  35.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  36.     private void initComponents() {
  37.  
  38.         txtNome = new javax.swing.JTextField();
  39.         jLabel1 = new javax.swing.JLabel();
  40.         jScrollPane1 = new javax.swing.JScrollPane();
  41.         jTablePesquisa = new javax.swing.JTable();
  42.         btPesquisa = new javax.swing.JButton();
  43.  
  44.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  45.  
  46.         txtNome.addActionListener(new java.awt.event.ActionListener() {
  47.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  48.                 txtNomeActionPerformed(evt);
  49.             }
  50.         });
  51.  
  52.         jLabel1.setText("Nome");
  53.  
  54.         jTablePesquisa.setModel(new javax.swing.table.DefaultTableModel(
  55.             new Object [][] {
  56.  
  57.             },
  58.             new String [] {
  59.  
  60.             }
  61.         ));
  62.         jScrollPane1.setViewportView(jTablePesquisa);
  63.  
  64.         btPesquisa.setText("Pesquisar");
  65.  
  66.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  67.         getContentPane().setLayout(layout);
  68.         layout.setHorizontalGroup(
  69.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  70.             .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 511, javax.swing.GroupLayout.PREFERRED_SIZE)
  71.             .addComponent(btPesquisa, javax.swing.GroupLayout.Alignment.TRAILING)
  72.             .addGroup(layout.createSequentialGroup()
  73.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.                     .addGroup(layout.createSequentialGroup()
  75.                         .addContainerGap()
  76.                         .addComponent(jLabel1))
  77.                     .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
  78.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  79.         );
  80.         layout.setVerticalGroup(
  81.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.             .addGroup(layout.createSequentialGroup()
  83.                 .addComponent(jLabel1)
  84.                 .addGap(11, 11, 11)
  85.                 .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  86.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  87.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
  88.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  89.                 .addComponent(btPesquisa))
  90.         );
  91.  
  92.         pack();
  93.     }// </editor-fold>                        
  94.  
  95.     private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {                                        
  96.         // TODO add your handling code here:
  97.     }                                      
  98.  
  99.    @SuppressWarnings("empty-statement")
  100.     public void preencherTabela() {
  101.         ArrayList dados = new ArrayList();
  102.         String[] colunas = new String[]{"ID", "NOME", "CPF", "CURSO"};
  103.         String SQL = "select id_aluno, nome, cpf, curso FROM aluno where aluno.nome like '%"+this.txtNome.getText()+"%'order by id_aluno";
  104.    
  105.          
  106.         try {
  107.            
  108.             Statement stm = conexao.createStatement();
  109.             ResultSet rs = stm.executeQuery(SQL);
  110.            
  111.             while (rs.next()) {
  112.                 dados.add(new Object[]{
  113.                     rs.getLong("id_aluno"),
  114.                     rs.getString("nome"),
  115.                     rs.getString("cpf"),
  116.                     rs.getString("curso")});
  117.             }
  118.  
  119.         } catch (Exception e) {
  120.             JOptionPane.showMessageDialog(null, "Erro no ArrayList! \nErro: "+e);
  121.         }
  122.        
  123.         ModeloTabela modelo = new ModeloTabela(dados, colunas);
  124.         jTablePesquisa.setModel(modelo);
  125.         jTablePesquisa.getColumnModel().getColumn(0).setPreferredWidth(80);
  126.         jTablePesquisa.getColumnModel().getColumn(0).setResizable(true);
  127.         jTablePesquisa.getColumnModel().getColumn(1).setPreferredWidth(227);
  128.         jTablePesquisa.getColumnModel().getColumn(1).setResizable(true);
  129.         jTablePesquisa.getColumnModel().getColumn(2).setPreferredWidth(100);
  130.         jTablePesquisa.getColumnModel().getColumn(2).setResizable(true);
  131.         jTablePesquisa.getColumnModel().getColumn(3).setPreferredWidth(220);
  132.         jTablePesquisa.getColumnModel().getColumn(3).setResizable(true);
  133.         jTablePesquisa.getTableHeader().setReorderingAllowed(false);
  134.         jTablePesquisa.setAutoResizeMode(jTablePesquisa.AUTO_RESIZE_OFF);
  135.         jTablePesquisa.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  136.  
  137.     }
  138.     public static void main(String args[]) {
  139.         /* Set the Nimbus look and feel */
  140.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  141.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  142.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  143.          */
  144.         try {
  145.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  146.                 if ("Nimbus".equals(info.getName())) {
  147.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  148.                     break;
  149.                 }
  150.             }
  151.         } catch (ClassNotFoundException ex) {
  152.             java.util.logging.Logger.getLogger(FormAlterar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  153.         } catch (InstantiationException ex) {
  154.             java.util.logging.Logger.getLogger(FormAlterar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  155.         } catch (IllegalAccessException ex) {
  156.             java.util.logging.Logger.getLogger(FormAlterar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  157.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  158.             java.util.logging.Logger.getLogger(FormAlterar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  159.         }
  160.         //</editor-fold>
  161.  
  162.         /* Create and display the form */
  163.         java.awt.EventQueue.invokeLater(new Runnable() {
  164.             public void run() {
  165.                 new FormAlterar().setVisible(true);
  166.             }
  167.         });
  168.     }
  169.  
  170.     // Variables declaration - do not modify                    
  171.     private javax.swing.JButton btPesquisa;
  172.     private javax.swing.JLabel jLabel1;
  173.     private javax.swing.JScrollPane jScrollPane1;
  174.     private javax.swing.JTable jTablePesquisa;
  175.     private javax.swing.JTextField txtNome;
  176.     // End of variables declaration                  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement