Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package exemploImportacaoDeDados;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- import java.util.ArrayList;
- /**
- *
- * @author Gastao
- */
- public class ImportacaoDeDadosSwing extends javax.swing.JFrame {
- ArrayList<Pessoa> ListaDePessoasImportadas = new ArrayList<Pessoa>();
- int PosicaoNaListaDePessoasImportadas = -1;
- /**
- * Creates new form ImportacaoDeDadosSwing
- */
- public ImportacaoDeDadosSwing() {
- initComponents();
- }
- public void mostrarDadosImportados(int Posicao) {
- if (!ListaDePessoasImportadas.isEmpty()) {
- Pessoa pes = ListaDePessoasImportadas.get(Posicao);
- edtNome.setText(pes.getNome());
- edtTelefone.setText(pes.getTelefone());
- }
- atualizarPosicaoTela();
- }
- public void atualizarPosicaoTela(){
- lblContador.setText( PosicaoNaListaDePessoasImportadas + "/" + (ListaDePessoasImportadas.size()-1) );
- }
- /**
- * 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() {
- jButton2 = new javax.swing.JButton();
- btnImportarArquivo = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- edtNome = new javax.swing.JTextField();
- jLabel2 = new javax.swing.JLabel();
- edtTelefone = new javax.swing.JTextField();
- jButton4 = new javax.swing.JButton();
- jButton5 = new javax.swing.JButton();
- jButton6 = new javax.swing.JButton();
- jButton7 = new javax.swing.JButton();
- lblContador = new javax.swing.JLabel();
- jButton2.setText("jButton2");
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- btnImportarArquivo.setText("Importar Arquivo");
- btnImportarArquivo.setName("btnImportarArquivo");
- btnImportarArquivo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnImportarArquivoActionPerformed(evt);
- }
- });
- jLabel1.setText("Nome:");
- jLabel1.setName("lblNome");
- edtNome.setName("edtNome");
- jLabel2.setText("Telefone:");
- jLabel2.setName("lblTel");
- edtTelefone.setName("edtTelefone");
- jButton4.setText("<<");
- jButton4.setName("btnPrimeiro");
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jButton5.setText("<");
- jButton5.setName("btnAnterior");
- jButton5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton5ActionPerformed(evt);
- }
- });
- jButton6.setText(">");
- jButton6.setName("btnProximo");
- jButton6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton6ActionPerformed(evt);
- }
- });
- jButton7.setLabel(">>");
- jButton7.setName("btnUltimo");
- jButton7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton7ActionPerformed(evt);
- }
- });
- lblContador.setText(".:.");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(btnImportarArquivo)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton4)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton5)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton6)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton7)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(lblContador))
- .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel2)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(edtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(edtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)))))
- .addGap(0, 139, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(edtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(edtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(39, 39, 39)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btnImportarArquivo)
- .addComponent(jButton4)
- .addComponent(jButton5)
- .addComponent(jButton6)
- .addComponent(jButton7)
- .addComponent(lblContador))
- .addContainerGap(205, Short.MAX_VALUE))
- );
- edtNome.getAccessibleContext().setAccessibleName("edtNome");
- pack();
- }// </editor-fold>
- private void btnImportarArquivoActionPerformed(java.awt.event.ActionEvent evt) {
- ListaDePessoasImportadas.clear();
- PosicaoNaListaDePessoasImportadas = -1;
- JFileChooser f = new JFileChooser();
- int returnVal = f.showOpenDialog(this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File file = f.getSelectedFile();
- try {
- BufferedReader in = new BufferedReader(new FileReader(file.getPath()));
- String linha;
- while (in.ready()) {
- linha = in.readLine();
- String s[] = linha.split("\\;");
- Pessoa pes = new Pessoa();
- pes.setNome(s[0]);
- pes.setTelefone(s[1]);
- ListaDePessoasImportadas.add(pes);
- }
- in.close();
- JOptionPane.showMessageDialog(this, "Importação realizada com sucesso");
- atualizarPosicaoTela();
- } catch (IOException e) {
- JOptionPane.showMessageDialog(this, "Problemas na leitura do arquivo:" + e.getMessage());
- }
- } else {
- JOptionPane.showMessageDialog(this, "Comando cancelado");
- }
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- PosicaoNaListaDePessoasImportadas = 0;
- mostrarDadosImportados(PosicaoNaListaDePessoasImportadas);
- }
- private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
- PosicaoNaListaDePessoasImportadas -= 1;
- if (PosicaoNaListaDePessoasImportadas < 0) {
- PosicaoNaListaDePessoasImportadas = 0;
- }
- mostrarDadosImportados(PosicaoNaListaDePessoasImportadas);
- }
- private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
- PosicaoNaListaDePessoasImportadas += 1;
- if (PosicaoNaListaDePessoasImportadas > (ListaDePessoasImportadas.size() - 1)) {
- PosicaoNaListaDePessoasImportadas = ListaDePessoasImportadas.size() - 1;
- }
- mostrarDadosImportados(PosicaoNaListaDePessoasImportadas);
- }
- private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
- PosicaoNaListaDePessoasImportadas = ListaDePessoasImportadas.size() - 1;
- mostrarDadosImportados(PosicaoNaListaDePessoasImportadas);
- }
- /**
- * @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(ImportacaoDeDadosSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(ImportacaoDeDadosSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(ImportacaoDeDadosSwing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(ImportacaoDeDadosSwing.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 ImportacaoDeDadosSwing().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnImportarArquivo;
- private javax.swing.JTextField edtNome;
- private javax.swing.JTextField edtTelefone;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton4;
- private javax.swing.JButton jButton5;
- private javax.swing.JButton jButton6;
- private javax.swing.JButton jButton7;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel lblContador;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement