Results 1 to 5 of 5
Thread: List models awesome usage -.-''
- 12-21-2011, 08:10 PM #1
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:
Afterward I've adding them to my panel by writing:Java 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(); } }
But when I call the properly method textsLm.add(something) the List doesn't display the correct information.Java 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);
What's wrong? :(
- 12-21-2011, 08:26 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.Last edited by pbrockway2; 12-21-2011 at 08:37 PM.
- 12-21-2011, 08:48 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 12-22-2011, 11:23 AM #4
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:
I haven't post an SSCCE because the situation are very hugeJava Code:this.fireContentsChanged(this, 0, texts.size());
.gif)
But unlike you say also extending DefaultListModel I must call
Generally I must put in my mind to call fireXXX() everywhere I use a model for MVC representation of data in my java apps.Java Code:this.fireContentsChanged(this, 0, texts.size());
I'm a bit fast from java
I will treasure of examples you posted to me
Very very thanks, I solved!
- 12-22-2011, 08:40 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
New Poster-Need Awesome Java Engineer - External Referral Bonus!!!
By Haley in forum Jobs OfferedReplies: 0Last Post: 09-17-2010, 02:03 AM -
Java Swing Tables ( JTable Models ) to connect to Database using Table Models
By javaprogrammerishere in forum AWT / SwingReplies: 2Last Post: 01-27-2010, 08:28 AM -
Usage of List
By vasavi.singh in forum New To JavaReplies: 0Last Post: 03-12-2009, 02:51 PM -
Testing EMF models.
By manik_jforum in forum EclipseReplies: 0Last Post: 12-10-2008, 08:59 AM -
Pretty awesome - James Gosling receives Sexy Programmer Title
By tim marcus in forum EntertainmentReplies: 6Last Post: 10-23-2008, 11:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks