Differences and uses of javax.swing.JList and javax.swing.DefaultListModel [closed]

0

I have been doing a small application where I am using lists and I hit with defaultlistmodel and observing I have seen that they only change in the way of declaring the properties. The issue is that in addition to this there are other differences in the use of each one. , and also how it is possible that from a list of options when selecting some these are displayed in another independent list.

    
asked by Ludwig111 21.11.2017 в 23:10
source

1 answer

-1

As mentioned by the comrade in the commentary, a JList is a "lightweight" component (ie, lightweight), which is visually displayed and can be added to a container (such as a < strong> JPanel or a JFrame ). A JList and a DefaultListModel are completely different things.

A JList is a component, and a DefaultListModel is a model that can be used in that component.

A model is the way you are going to organize your list, plus it is the one that stores the objects that you add (the NO objects are stored in the < strong> JList , they are stored in the model that has that component).

Now, JList has a model type by default, and that model is the ListModel interface, which includes certain methods other than DefaultListModel strong>. Why?

This is because the ListModel interface is used almost only to get the values of a JList , as indicated in the Java documentation for a ListModel .

On the other hand, the DefaultListModel is more of a class that allows you to perform more actions (such as add, obtain, delete, adjust), as indicated in Java documentation for a DefaultListModel .

In addition to this, there are other types of models that can be used as required.

That said, the way you can modify the model of a JList component is by using the JList.setModel(tuModelo); method, where "yourModel" is an instance of the model you want to use. For example, if you wanted to modify the model of your JList (a ListModel ) for a DefaultListModel , you would simply do something like this:

DefaultListModel miModelo = new DefaultListModel(); // se instancia el nuevo modelo
// puedes utilizar el método miModelo.add(miObjeto); para agregar objetos al modelo antes ser ajustado al JList
JList.setModel(miModelo); // ajusta el nuevo modelo a tu JList

This way, the model of your JList will be changed.

    
answered by 27.11.2017 / 06:22
source