DefaultListModel in Java
Asked Answered
S

3

7

What is DefaultListModel in Java? and What is its purpose? I tried to google it but didn't get any proper explanation for it!

Sclar answered 30/10, 2012 at 10:15 Comment(1)
all data for view (JList) are stored in model (XxxListModel), basically all JComponents have got own model,Heraclea
E
4

DefaultListModel is a implementation of the ListModel that extends from AbstractListModel.

Internally, it is backed by List implementation.

It's intention is to provide a simple implementation of the ListModel that you can use for modelling simple data models where having to extend from the AbstractListModel or implement the ListModel interface would be overkill for accomplishing the same task

Epidermis answered 30/10, 2012 at 10:24 Comment(0)
B
7

It works as a Model (predefined) for JList

Swings follows MVC and DefaultListModel plays its part as M in MVC.

There are 3 ways to create model:

  • DefaultListModel

    Everything is pretty much taken care of by language framework.

  • AbstractListModel

    You manage the data and invoke the "fire" methods. For this approach, you must subclass AbstractListModel and implement the getSize and getElementAt methods inherited from the ListModel interface.

  • ListModel

    You manage everything.

Brita answered 30/10, 2012 at 10:25 Comment(1)
Dead links on this answer.Exemplification
S
5

Here's a link to an example of using ListModel and DefaultListModel, its the second link when you google it.

http://www.java2s.com/Code/Java/Swing-JFC/AnexampleofJListwithaDefaultListModel.htm

Shelter answered 30/10, 2012 at 10:21 Comment(0)
E
4

DefaultListModel is a implementation of the ListModel that extends from AbstractListModel.

Internally, it is backed by List implementation.

It's intention is to provide a simple implementation of the ListModel that you can use for modelling simple data models where having to extend from the AbstractListModel or implement the ListModel interface would be overkill for accomplishing the same task

Epidermis answered 30/10, 2012 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.