0

I want to add an object to a JList in java. The object has 4 variables which need to be in a single row of JList (represent the values within one object) --> meaning that each object will have its information listed in a single row. I've tried with the default list model but an item gets inserted in JList in this format: classOfObject@1cf3d4e

public  static void addElementToList(JList list, Object obj){


   DefaultListModel dlm = new DefaultListModel();

   int size = list.getModel().getSize();
   for (int i=0;i<size;i++){

      dlm.addElement(list.getModel().getElementAt(i));

  }
   dlm.addElement(obj);
   list.setModel(dlm);
}

And I get this as a result:
enter image description here

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You're seeing Object's default `public String toString()` in your JList. The quick and dirty way to solve this is to override your Airline class's toString() method to return something that you want to display, but the better solution is as per Camickr's answer in the 2nd duplicate -- to use a custom renderer. – Hovercraft Full Of Eels May 26 '20 at 23:06
  • Yeepp it works. I overrided the to string method and know is okay it displays the info the objects have. Thank you very much – Kevin Lesha May 27 '20 at 00:11

0 Answers0