Advertisement
gastaojunior

Importacao de dados Pessoa

Mar 19th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.16 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package exemploImportacaoDeDados;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.FileReader;
  10. import java.io.IOException;
  11. import javax.swing.JFileChooser;
  12. import javax.swing.JOptionPane;
  13. import java.util.ArrayList;
  14.  
  15. /**
  16.  *
  17.  * @author Gastao
  18.  */
  19. public class ImportacaoDeDadosSwing extends javax.swing.JFrame {
  20.  
  21.     ArrayList<Pessoa> ListaDePessoasImportadas = new ArrayList<Pessoa>();
  22.     int PosicaoNaListaDePessoasImportadas = -1;
  23.  
  24.     /**
  25.      * Creates new form ImportacaoDeDadosSwing
  26.      */
  27.     public ImportacaoDeDadosSwing() {
  28.         initComponents();
  29.     }
  30.  
  31.     public void mostrarDadosImportados(int Posicao) {
  32.  
  33.         if (!ListaDePessoasImportadas.isEmpty()) {
  34.             Pessoa pes = ListaDePessoasImportadas.get(Posicao);
  35.  
  36.  
  37.             edtNome.setText(pes.getNome());
  38.             edtTelefone.setText(pes.getTelefone());        
  39.            
  40.         }
  41.         atualizarPosicaoTela();
  42.     }
  43.    
  44.     public void atualizarPosicaoTela(){
  45.         lblContador.setText( PosicaoNaListaDePessoasImportadas + "/" + (ListaDePessoasImportadas.size()-1)  );
  46.     }
  47.  
  48.     /**
  49.      * This method is called from within the constructor to initialize the form.
  50.      * WARNING: Do NOT modify this code. The content of this method is always
  51.      * regenerated by the Form Editor.
  52.      */
  53.     @SuppressWarnings("unchecked")
  54.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  55.     private void initComponents() {
  56.  
  57.         jButton2 = new javax.swing.JButton();
  58.         btnImportarArquivo = new javax.swing.JButton();
  59.         jLabel1 = new javax.swing.JLabel();
  60.         edtNome = new javax.swing.JTextField();
  61.         jLabel2 = new javax.swing.JLabel();
  62.         edtTelefone = new javax.swing.JTextField();
  63.         jButton4 = new javax.swing.JButton();
  64.         jButton5 = new javax.swing.JButton();
  65.         jButton6 = new javax.swing.JButton();
  66.         jButton7 = new javax.swing.JButton();
  67.         lblContador = new javax.swing.JLabel();
  68.  
  69.         jButton2.setText("jButton2");
  70.  
  71.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  72.  
  73.         btnImportarArquivo.setText("Importar Arquivo");
  74.         btnImportarArquivo.setName("btnImportarArquivo");
  75.         btnImportarArquivo.addActionListener(new java.awt.event.ActionListener() {
  76.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  77.                 btnImportarArquivoActionPerformed(evt);
  78.             }
  79.         });
  80.  
  81.         jLabel1.setText("Nome:");
  82.         jLabel1.setName("lblNome");
  83.  
  84.         edtNome.setName("edtNome");
  85.  
  86.         jLabel2.setText("Telefone:");
  87.         jLabel2.setName("lblTel");
  88.  
  89.         edtTelefone.setName("edtTelefone");
  90.  
  91.         jButton4.setText("<<");
  92.         jButton4.setName("btnPrimeiro");
  93.         jButton4.addActionListener(new java.awt.event.ActionListener() {
  94.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  95.                 jButton4ActionPerformed(evt);
  96.             }
  97.         });
  98.  
  99.         jButton5.setText("<");
  100.         jButton5.setName("btnAnterior");
  101.         jButton5.addActionListener(new java.awt.event.ActionListener() {
  102.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  103.                 jButton5ActionPerformed(evt);
  104.             }
  105.         });
  106.  
  107.         jButton6.setText(">");
  108.         jButton6.setName("btnProximo");
  109.         jButton6.addActionListener(new java.awt.event.ActionListener() {
  110.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  111.                 jButton6ActionPerformed(evt);
  112.             }
  113.         });
  114.  
  115.         jButton7.setLabel(">>");
  116.         jButton7.setName("btnUltimo");
  117.         jButton7.addActionListener(new java.awt.event.ActionListener() {
  118.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  119.                 jButton7ActionPerformed(evt);
  120.             }
  121.         });
  122.  
  123.         lblContador.setText(".:.");
  124.  
  125.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  126.         getContentPane().setLayout(layout);
  127.         layout.setHorizontalGroup(
  128.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  129.             .addGroup(layout.createSequentialGroup()
  130.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  131.                     .addGroup(layout.createSequentialGroup()
  132.                         .addContainerGap()
  133.                         .addComponent(btnImportarArquivo)
  134.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  135.                         .addComponent(jButton4)
  136.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  137.                         .addComponent(jButton5)
  138.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  139.                         .addComponent(jButton6)
  140.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  141.                         .addComponent(jButton7)
  142.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  143.                         .addComponent(lblContador))
  144.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  145.                         .addGap(18, 18, 18)
  146.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  147.                             .addGroup(layout.createSequentialGroup()
  148.                                 .addComponent(jLabel2)
  149.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  150.                                 .addComponent(edtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE))
  151.                             .addGroup(layout.createSequentialGroup()
  152.                                 .addComponent(jLabel1)
  153.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  154.                                 .addComponent(edtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)))))
  155.                 .addGap(0, 139, Short.MAX_VALUE))
  156.         );
  157.         layout.setVerticalGroup(
  158.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  159.             .addGroup(layout.createSequentialGroup()
  160.                 .addGap(18, 18, 18)
  161.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  162.                     .addComponent(jLabel1)
  163.                     .addComponent(edtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  164.                 .addGap(18, 18, 18)
  165.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  166.                     .addComponent(jLabel2)
  167.                     .addComponent(edtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  168.                 .addGap(39, 39, 39)
  169.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  170.                     .addComponent(btnImportarArquivo)
  171.                     .addComponent(jButton4)
  172.                     .addComponent(jButton5)
  173.                     .addComponent(jButton6)
  174.                     .addComponent(jButton7)
  175.                     .addComponent(lblContador))
  176.                 .addContainerGap(205, Short.MAX_VALUE))
  177.         );
  178.  
  179.         edtNome.getAccessibleContext().setAccessibleName("edtNome");
  180.  
  181.         pack();
  182.     }// </editor-fold>                        
  183.  
  184.     private void btnImportarArquivoActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  185.         ListaDePessoasImportadas.clear();
  186.         PosicaoNaListaDePessoasImportadas = -1;
  187.  
  188.         JFileChooser f = new JFileChooser();
  189.         int returnVal = f.showOpenDialog(this);
  190.  
  191.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  192.             File file = f.getSelectedFile();
  193.             try {
  194.                 BufferedReader in = new BufferedReader(new FileReader(file.getPath()));
  195.                 String linha;
  196.                 while (in.ready()) {
  197.                     linha = in.readLine();
  198.                     String s[] = linha.split("\\;");
  199.  
  200.                     Pessoa pes = new Pessoa();
  201.                     pes.setNome(s[0]);
  202.                     pes.setTelefone(s[1]);
  203.  
  204.                     ListaDePessoasImportadas.add(pes);
  205.                 }
  206.                 in.close();
  207.                 JOptionPane.showMessageDialog(this, "Importação realizada com sucesso");
  208.                 atualizarPosicaoTela();
  209.  
  210.             } catch (IOException e) {
  211.                 JOptionPane.showMessageDialog(this, "Problemas na leitura do arquivo:" + e.getMessage());
  212.             }
  213.         } else {
  214.             JOptionPane.showMessageDialog(this, "Comando cancelado");
  215.         }
  216.  
  217.     }                                                  
  218.  
  219.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  220.         PosicaoNaListaDePessoasImportadas = 0;
  221.         mostrarDadosImportados(PosicaoNaListaDePessoasImportadas);
  222.        
  223.     }                                        
  224.  
  225.     private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  226.         PosicaoNaListaDePessoasImportadas -= 1;
  227.         if (PosicaoNaListaDePessoasImportadas < 0) {
  228.             PosicaoNaListaDePessoasImportadas = 0;
  229.         }
  230.         mostrarDadosImportados(PosicaoNaListaDePessoasImportadas);
  231.  
  232.     }                                        
  233.  
  234.     private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  235.         PosicaoNaListaDePessoasImportadas += 1;
  236.         if (PosicaoNaListaDePessoasImportadas > (ListaDePessoasImportadas.size() - 1)) {
  237.             PosicaoNaListaDePessoasImportadas = ListaDePessoasImportadas.size() - 1;
  238.         }
  239.         mostrarDadosImportados(PosicaoNaListaDePessoasImportadas);
  240.  
  241.     }                                        
  242.  
  243.     private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  244.         PosicaoNaListaDePessoasImportadas = ListaDePessoasImportadas.size() - 1;
  245.         mostrarDadosImportados(PosicaoNaListaDePessoasImportadas);
  246.  
  247.  
  248.     }                                        
  249.  
  250.     /**
  251.      * @param args the command line arguments
  252.      */
  253.     public static void main(String args[]) {
  254.         /*
  255.          * Set the Nimbus look and feel
  256.          */
  257.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  258.         /*
  259.          * If Nimbus (introduced in Java SE 6) is not available, stay with the
  260.          * default look and feel. For details see
  261.          * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  262.          */
  263.         try {
  264.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  265.                 if ("Nimbus".equals(info.getName())) {
  266.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  267.                     break;
  268.                 }
  269.             }
  270.         } catch (ClassNotFoundException ex) {
  271.             java.util.logging.Logger.getLogger(ImportacaoDeDadosSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  272.         } catch (InstantiationException ex) {
  273.             java.util.logging.Logger.getLogger(ImportacaoDeDadosSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  274.         } catch (IllegalAccessException ex) {
  275.             java.util.logging.Logger.getLogger(ImportacaoDeDadosSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  276.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  277.             java.util.logging.Logger.getLogger(ImportacaoDeDadosSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  278.         }
  279.         //</editor-fold>
  280.  
  281.         /*
  282.          * Create and display the form
  283.          */
  284.         java.awt.EventQueue.invokeLater(new Runnable() {
  285.  
  286.             public void run() {
  287.                 new ImportacaoDeDadosSwing().setVisible(true);
  288.             }
  289.         });
  290.     }
  291.     // Variables declaration - do not modify                    
  292.     private javax.swing.JButton btnImportarArquivo;
  293.     private javax.swing.JTextField edtNome;
  294.     private javax.swing.JTextField edtTelefone;
  295.     private javax.swing.JButton jButton2;
  296.     private javax.swing.JButton jButton4;
  297.     private javax.swing.JButton jButton5;
  298.     private javax.swing.JButton jButton6;
  299.     private javax.swing.JButton jButton7;
  300.     private javax.swing.JLabel jLabel1;
  301.     private javax.swing.JLabel jLabel2;
  302.     private javax.swing.JLabel lblContador;
  303.     // End of variables declaration                  
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement