Rename an element in a Jlist

Asked

Viewed 187 times

4

Greetings, my friends.

I have a problem in Java.

Na foto: O cadastro a ser realizado

I am trying to add a "Occurrence" element to a Jlist. This element has 4 elements within it, which I need to maintain. So far so good.

However, the problem appears when I add the said element to the list

O problema

The list displays this name in the widget. Why? I would like the name of the occurrence to appear instead of this name.

Here is the occurrence code:

 @Override
        public void actionPerformed(ActionEvent e) {
            //dispose();
            Ocorrencia ocorrencia = new Ocorrencia();
            ocorrencia.setLocationRelativeTo(null);
            ocorrencia.setVisible(true);
            ocorrenciasAtivas.modeloOcorrencias.addElement(ocorrencia);

        }

1 answer

3


You need to implement toString() in your class Ocorrencia.

This method should return a readable version of your object’s data such as String. How did you not do it, the JList used the standard implementation, which is String ugly that you’re seeing.

  • 1

    Excellent answer. Fast and practical. I replaced the class toString method by returning the name string. Thank you, Pablo!

Browser other questions tagged

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