How to print an ArrayList inside a JOptionPane? [duplicate]

0

I would like to know how to print an ArrayList inside a JOptionPane, I leave below the code I am using but it is not working:

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class repositorioPessoasLista{

    ArrayList<pessoa> pessoas = new ArrayList<pessoa>();
    pessoa Pessoa = new pessoa();


    public void inserir(){

        Pessoa.setNome(JOptionPane.showInputDialog("Digite o nome da pessoa: "));
        Pessoa.setCPF(JOptionPane.showInputDialog("Digite o CPF da pessoa: "));
        Pessoa.setIdade(Integer.parseInt(JOptionPane.showInputDialog("Digite a idade da pessoa: ")));

        pessoas.add(Pessoa);
      }

    public void pesquisar(int i){

        for(i=0 ; i < pessoas.size() ; i++){

            JOptionPane.showMessageDialog(null ,pessoas.get((i)));

          }



      }


}

and here is the class where I call the search function with a ActionListener

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class janela extends JFrame {

    static repositorioPessoasLista rep = new repositorioPessoasLista();
    private static final long serialVersionUID = 1L;

    protected static JFrame frame;
    protected static JMenuBar barraMenu;
    protected static JMenu Cadastro;
    protected static JMenu Pesquisa;
    protected static JMenu Excluir;
    protected static JMenu Sistema;
    protected static JMenuItem Professorbt;
    protected static JMenuItem Aluno;
    protected static JMenuItem ProfessorPesqbt;
    protected static JMenuItem AlunoPesq;
    protected static JMenuItem ProfessorExc;
    protected static JMenuItem AlunoExc;
    protected static JMenuItem Sobre;
    protected static JMenuItem Sair;



    public static void main(String[] args) {
        //FRAME
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,400);
        frame.setLocation(400,300);

        //BARRA DE MENU
        barraMenu = new JMenuBar();
        Cadastro = new JMenu("Cadastro");
        Pesquisa = new JMenu("Pesquisa");
        Excluir = new JMenu("Exluir");
        Sistema = new JMenu("Sistema");
        barraMenu.add(Cadastro);
        barraMenu.add(Pesquisa);
        barraMenu.add(Excluir);
        barraMenu.add(Sistema);

        //ITENS DE MENU
        Professorbt = new JMenuItem("Professor");
        Aluno = new JMenuItem("Aluno");
        ProfessorPesqbt = new JMenuItem("Professor");
        AlunoPesq = new JMenuItem("Aluno");
        ProfessorExc = new JMenuItem("Professor");
        AlunoExc = new JMenuItem("Aluno");
        Sobre = new JMenuItem("Sobre");
        Sair = new JMenuItem("Sair");
        Cadastro.add(Professorbt);
        Cadastro.add(Aluno);
        Pesquisa.add(ProfessorPesqbt);
        Pesquisa.add(AlunoPesq);
        Excluir.add(ProfessorExc);
        Excluir.add(AlunoExc);
        Sistema.add(Sobre);
        Sistema.add(Sair);

        frame.add(barraMenu);
        frame.add(barraMenu, BorderLayout.NORTH);
        frame.setVisible(true);

        //BOTAO SAIR
        Sair.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){

                int retorno = JOptionPane.showConfirmDialog(null, "Deseja sair ?"," ",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

                if(retorno == JOptionPane.YES_OPTION){

                    System.exit(0);

                   }

              }


        });

        //BOTAO SOBRE
        Sobre.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){

                JOptionPane.showMessageDialog(null,"SOBRE SISTEMA\n\nDesenvolvedor: Douglas Sabino\nVersão: 1.01(BETA)");

              }


        });

        //BOTAO CADASTRAR
        Professorbt.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){

                rep.inserir();

              }

        });

        ProfessorPesqbt.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){

                rep.pesquisar(0);

              }

        });

    }









}
    
asked by anonymous 20.05.2017 / 23:29

1 answer

1

I'd like to give you two suggestions:

  

Try to follow java standardization correctly:

     
  • methods should always be started with minuscule, following the pattern CamelCase for compound names in> and

  •   
  • classes should always start with capital letter, also following the CamelCase pattern, in case of compound names.

      

Another thing is

     
  • Always start the screen within the Event-Dispatch-Thread ,   because swing is not Thread-Safe , and the whole GUI needs to start in.   of this unique Thread. Nesta   answer better explains the   reason for this and possible problems that may occur. This Other   answer shows some   ways to start the application within this Thread.
  •   

It's not very clear what "is not working" in your question, so it's important that you always clearly explain the problem of the code, making it easier for anyone to respond.

However, I think the problem is when it is time to display, notice that pessoas.get(i) will retrieve an object of type pessoa , and if you do not overwrite the toString() method, the return will be nomeDaClasse@hascode , where hashcode is generated by java itself.

Try to override this method in the person class this way:

@Override
public String toString(){

    return "Nome: " + this.nome + " Idade: " + this.idade + " CPF: " + this.CPF;
}
    
21.05.2017 / 00:25