• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Add objects to DefaultListModel?  RSS feed

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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: 78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved. I needed to create a object before I added it to the list.
 
Saloon Keeper
Posts: 2086
76
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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: 78
  • Mark post as helpful
  • send pies
  • 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: 62231
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
A timing clock, fuse wire, high explosives and a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!