Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default 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:

    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();
    	}
    }
    Afterward I've adding them to my panel by writing:

    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);
    But when I call the properly method textsLm.add(something) the List doesn't display the correct information.

    What's wrong? :(

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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 09:37 PM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

  4. #4
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default 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:
    Java Code:
    this.fireContentsChanged(this, 0, texts.size());
    I haven't post an SSCCE because the situation are very huge

    But unlike you say also extending DefaultListModel I must call
    Java 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

    I will treasure of examples you posted to me

    Very very thanks, I solved!

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Replies: 0
    Last Post: 09-17-2010, 03:03 AM
  2. Replies: 2
    Last Post: 01-27-2010, 09:28 AM
  3. Usage of List
    By vasavi.singh in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 03:51 PM
  4. Testing EMF models.
    By manik_jforum in forum Eclipse
    Replies: 0
    Last Post: 12-10-2008, 09:59 AM
  5. Replies: 6
    Last Post: 10-23-2008, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •