• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Add objects to DefaultListModel?

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I use a DefaultListModel to add my own objects?  
My object that i want to add to the DefaultListModel looks like this


I tried to declare it like this:

Then  i tried to add my object to the list:

When trying to “addElement” my IDE tells me that:

The method addElement(Person) in the type DefaultListModel <Person> is not applicable for arguments (String, int)






 
Daniel Andersson
Ranch Hand
Posts: 113
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem solved. I needed to create a object before I added it to the list.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing the solution. Just for clarity, your code can also be split as:
Assuming getListModelAffär() returns a DefaultListModel<Person>
 
Daniel Andersson
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A new problem came up. :/

I’m trying to add my own objects to a DefaultListModel and then to show the list using a JList.

I now do have some code working for adding my object to the DefaultListModel but every time I add a new object, every other object in the list becomes the new object.

Maybe someone can look at my code and see where the adding of the objects goes wrong.

I think this is the relevant code for this problem.
AddListenerShop.java
 
Gui.java
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Andersson wrote:. . . My object that i want to add to the DefaultListModel looks like this
. . .

But that isn't an object. Apart from the error you made making those fields public, the error of making them static is worse. I shall let you find out what will go wrong for yourself
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic