Percorrer Lista, erro de lógica. [RESOLVIDO]

  1. O código dos eventos dos botões estão funcionando normalmente. Acontece que vc está inserindo o aluno duas vezes na lista, então vc tem a impressão de que vc tem que clicar no botão duas vezes pra funcionar.
    Veja abaixo:

[code]
import javax.swing.JOptionPane;

public class CadAluno extends javax.swing.JFrame {

public CadAluno(Lista<Aluno> listaAlunos) {
    initComponents();
    this.listaAlunos = listaAlunos;
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    txtNome = new javax.swing.JTextField();
    txtMatricula = new javax.swing.JTextField();
    cmbCurso = new javax.swing.JComboBox();
    jPanel1 = new javax.swing.JPanel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    txtLogradouro = new javax.swing.JTextField();
    txtComplemento = new javax.swing.JTextField();
    cmbCidade = new javax.swing.JComboBox();
    btOk = new javax.swing.JButton();
    btCancelar = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Cadastro Aluno");

    jLabel1.setText("Nome:");

    jLabel2.setText("Curso");

    jLabel3.setText("Matrícula:");

    txtNome.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtNomeActionPerformed(evt);
        }
    });

    cmbCurso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administração", "Informática", "Direito" }));

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Endereço"));

    jLabel4.setText("Logradouro");

    jLabel5.setText("Complemento");

    jLabel6.setText("Cidade");

    cmbCidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Porto Alegre", "São Paulo", "Rio de Janeiro" }));

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel4)
                .addComponent(jLabel5)
                .addComponent(jLabel6))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(cmbCidade, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtComplemento)
                .addComponent(txtLogradouro, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
            .addContainerGap(222, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4)
                .addComponent(txtLogradouro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(txtComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel6)
                .addComponent(cmbCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(31, Short.MAX_VALUE))
    );

    btOk.setText("OK");
    btOk.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btOkActionPerformed(evt);
        }
    });

    btCancelar.setText("Cancelar");
    btCancelar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btCancelarActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(33, 33, 33)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtMatricula, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtNome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                    .addGap(17, 17, 17)
                    .addComponent(jLabel2)
                    .addGap(18, 18, 18)
                    .addComponent(cmbCurso, 0, 192, Short.MAX_VALUE)))
            .addContainerGap())
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(341, Short.MAX_VALUE)
            .addComponent(btOk)
            .addGap(27, 27, 27)
            .addComponent(btCancelar)
            .addGap(45, 45, 45))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(44, 44, 44)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(19, 19, 19)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(jLabel2)
                .addComponent(txtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(cmbCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(40, 40, 40)
            .addComponent(jPanel1, 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(btCancelar)
                .addComponent(btOk))
            .addContainerGap(34, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>



private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {


}



private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {
    dispose();

}

private void btOkActionPerformed(java.awt.event.ActionEvent evt) {
    String nome = txtNome.getText();
    long matricula = Long.parseLong(txtMatricula.getText());
    String curso = cmbCurso.getSelectedItem().toString();
    String logradouro = txtLogradouro.getText();
    String complemento = txtComplemento.getText();
    String cidade = cmbCidade.getSelectedItem().toString();

    Endereco ed = new Endereco(logradouro, complemento, cidade);
    Aluno alu = new Aluno(nome, curso, matricula, ed);

    //listaAlunos.adicionar(alu); ->>> AQUI NESTA LINHA. SÓ COMENTEI ELA.


    if(listaAlunos.adicionar(alu) == true) //AQUI VC ESTARIA ADICIONANDO NOVAMENTE O MESMO ALUNO NA LISTA
        JOptionPane.showMessageDialog(null, "Adicionou a Lista");
       else
            JOptionPane.showMessageDialog(null, "Não Adicionou");


}



// Variables declaration - do not modify
private javax.swing.JButton btCancelar;
private javax.swing.JButton btOk;
private javax.swing.JComboBox cmbCidade;
private javax.swing.JComboBox cmbCurso;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField txtComplemento;
private javax.swing.JTextField txtLogradouro;
private javax.swing.JTextField txtMatricula;
private javax.swing.JTextField txtNome;
// End of variables declaration
private Lista<Aluno> listaAlunos;

}[/code]

T+

Obrigado pela ajuda amigo, até a próxima.

disponha :smiley: