Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package frames;
- import banco.Connect;
- import banco.ModeloTabela;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- import javax.swing.ListSelectionModel;
- public class FormAlterar extends javax.swing.JFrame {
- Connection conexao = null;
- public FormAlterar() {
- initComponents();
- try {
- Connect.conectar();
- } catch (ClassNotFoundException ex) {
- JOptionPane.showMessageDialog(null, "Erro ao conectar!\n Erro:" +ex);
- }
- preencherTabela( );
- }
- /**
- * 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">
- private void initComponents() {
- txtNome = new javax.swing.JTextField();
- jLabel1 = new javax.swing.JLabel();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTablePesquisa = new javax.swing.JTable();
- btPesquisa = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- txtNome.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- txtNomeActionPerformed(evt);
- }
- });
- jLabel1.setText("Nome");
- jTablePesquisa.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- },
- new String [] {
- }
- ));
- jScrollPane1.setViewportView(jTablePesquisa);
- btPesquisa.setText("Pesquisar");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 511, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btPesquisa, javax.swing.GroupLayout.Alignment.TRAILING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1))
- .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addGap(11, 11, 11)
- .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(btPesquisa))
- );
- pack();
- }// </editor-fold>
- private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- @SuppressWarnings("empty-statement")
- public void preencherTabela() {
- ArrayList dados = new ArrayList();
- String[] colunas = new String[]{"ID", "NOME", "CPF", "CURSO"};
- String SQL = "select id_aluno, nome, cpf, curso FROM aluno where aluno.nome like '%"+this.txtNome.getText()+"%'order by id_aluno";
- try {
- Statement stm = conexao.createStatement();
- ResultSet rs = stm.executeQuery(SQL);
- while (rs.next()) {
- dados.add(new Object[]{
- rs.getLong("id_aluno"),
- rs.getString("nome"),
- rs.getString("cpf"),
- rs.getString("curso")});
- }
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Erro no ArrayList! \nErro: "+e);
- }
- ModeloTabela modelo = new ModeloTabela(dados, colunas);
- jTablePesquisa.setModel(modelo);
- jTablePesquisa.getColumnModel().getColumn(0).setPreferredWidth(80);
- jTablePesquisa.getColumnModel().getColumn(0).setResizable(true);
- jTablePesquisa.getColumnModel().getColumn(1).setPreferredWidth(227);
- jTablePesquisa.getColumnModel().getColumn(1).setResizable(true);
- jTablePesquisa.getColumnModel().getColumn(2).setPreferredWidth(100);
- jTablePesquisa.getColumnModel().getColumn(2).setResizable(true);
- jTablePesquisa.getColumnModel().getColumn(3).setPreferredWidth(220);
- jTablePesquisa.getColumnModel().getColumn(3).setResizable(true);
- jTablePesquisa.getTableHeader().setReorderingAllowed(false);
- jTablePesquisa.setAutoResizeMode(jTablePesquisa.AUTO_RESIZE_OFF);
- jTablePesquisa.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- }
- 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(FormAlterar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(FormAlterar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(FormAlterar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(FormAlterar.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 FormAlterar().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btPesquisa;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTable jTablePesquisa;
- private javax.swing.JTextField txtNome;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement