List models awesome usage -.-''
I've implemented a JList that will list an ArrayList that changes it's content dinamically.
Then, as suggested, I've make it using a model like this:
Code:
public class TextsListModel extends AbstractListModel<TextsWrapper>
implements Collection<TextsWrapper>
{
/**
*
*/
private static final long serialVersionUID = 5971875615507695748L;
private ArrayList<TextsWrapper> texts;
/**
*
*/
public TextsListModel()
{
this.texts = new ArrayList<TextsWrapper>(
ApplicationSettings.DEFAULT_ARRAYLISTS_SIZE);
}
@Override
public int getSize()
{
return texts.size();
}
@Override
public TextsWrapper getElementAt(int index)
{
return texts.get(index);
}
@Override
public int size()
{
return texts.size();
}
@Override
public boolean isEmpty()
{
return texts.isEmpty();
}
@Override
public boolean contains(Object o)
{
return texts.contains(o);
}
@Override
public Iterator<TextsWrapper> iterator()
{
return texts.iterator();
}
@Override
public Object[] toArray()
{
return texts.toArray();
}
@Override
public <T> T[] toArray(T[] a)
{
return texts.toArray(a);
}
@Override
public boolean add(TextsWrapper e)
{
return texts.add(e);
}
@Override
public boolean remove(Object o)
{
return texts.remove(o);
}
@Override
public boolean containsAll(Collection<?> c)
{
return texts.containsAll(c);
}
@Override
public boolean addAll(Collection<? extends TextsWrapper> c)
{
return texts.addAll(c);
}
@Override
public boolean removeAll(Collection<?> c)
{
return texts.removeAll(c);
}
@Override
public boolean retainAll(Collection<?> c)
{
return texts.retainAll(c);
}
@Override
public void clear()
{
texts.clear();
}
}
Afterward I've adding them to my panel by writing:
Code:
TextsListModel textsLm = new TextsListModel();
JList<TextsWrapper> textsL = new JList<TextsWrapper>();
JScrollPane textsLsp = new JScrollPane(textsL);
textsL.setModel(textsLm);
textsL.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
textsL.setFixedCellWidth(50);
textsL.setLayoutOrientation(JList.VERTICAL);
textsL.setCellRenderer(new TextsListRenderer());
this.add(textsLsp);
But when I call the properly method textsLm.add(something) the List doesn't display the correct information.
What's wrong? :(
Re: List models awesome usage -.-''
Does the list model code compile? In particular what is AbstractListModel<TextsWrapper>?
[Edit] Likewise JList<TextsWrapper>. Perhaps you could post a SSCCE that illustrates the problem. ie without the renderer etc and with some stub representing the TextsWrapper class.
What makes an AbstractListModel different from, say, a DefaultListModel, is that you have to invoke the fireXXX() methods at the appropriate time. Otherwise classes that depend on the list's data - like the JList - will not find out that things have changed.
Re: List models awesome usage -.-''
There's an example of how the overridden methods call the fireXXX() methods at java2s.com. That example shows how a list/JList can be *sorted* which is behaviour the standard list models don't support. If your only intention is to add/remove elements at runtime, the DefaultListModel supports that. See, eg, the example and commentary on the How to Use Lists page of Oracle's Tutorial.
Re: List models awesome usage -.-''
You're right!
Then, the situation are a bit tricky.
I'm developing over java 7, but as a paper reference I've a Core Java for 5 version.
In example provided there isn't the line:
Code:
this.fireContentsChanged(this, 0, texts.size());
I haven't post an SSCCE because the situation are very huge :(yawn):
But unlike you say also extending DefaultListModel I must call
Code:
this.fireContentsChanged(this, 0, texts.size());
Generally I must put in my mind to call fireXXX() everywhere I use a model for MVC representation of data in my java apps.
I'm a bit fast from java :s:
I will treasure of examples you posted to me :o:
Very very thanks, I solved!
Re: List models awesome usage -.-''