How to print an Arraylist inside a Joptionpane?

Asked

Viewed 1,381 times

0

Staff would like to know how to print an Arraylist inside a Joptionpane, I leave down the code I am using but 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 function search 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);

              }

        });

    }









}
  • By the way, what is this parameter there for? It may be the culprit. Remove this parameter from the search class and run the code.

  • Douglas, when adding code, please format it by selecting it and clicking on {}.

1 answer

1


Before I would like to give 2 suggestions:

Try to follow the java standardization correctly:

  • methods should always be started with minuscule, following the standard Camelcase for compound names and

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

Another thing is

  • Always start the screen inside the Event-Dispatch-Thread, because swing is not Thread-Safe, and the entire GUI needs to start in this one Thread. In this reply explains better the reason for this and any problems that may occur. This other reply shows some ways to start the application within this thread.

It is not very clear what "is not working" in your question, so it is important that you always explain clearly what the problem of the code, facilitating the work of those who will answer.

However, I believe that the problem is at the time of showing, realize that pessoas.get(i) will retrieve an object of the type pessoa, and if you do not overwrite the method toString(), the return will be nomeDaClasse@hascode, where this hashcode is generated by the java itself.

Try overwriting this method in the person class this way:

@Override
public String toString(){

    return "Nome: " + this.nome + " Idade: " + this.idade + " CPF: " + this.CPF;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.