Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Add objects to DefaultListModel?  RSS feed

 
Ranch Hand
Posts: 104
  • 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: 104
  • 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.
 
Bartender
Posts: 2163
92
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: 104
  • 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: 63314
205
  • 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
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!