Results 1 to 4 of 4
  1. #1
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default Strange JList Problem

    I have a JList backed by a home-brew ListModel based on the JDK's DefaultListModel except my version uses ArrayList instead of Vector. My model carries a comparator
    reference and includes a sort() method.

    Java Code:
         /**
          * Sort the list
          * 
          * @throws IllegalStateException if no comparator has been specified
          */
         public void sort() throws IllegalStateException {
    
            if (comparator == null) {
                throw new IllegalStateException("No comparator has been specified");
            }
    
            delegate.sort(comparator);
            sorted = true;
            this.fireContentsChanged(this, 0, delegate.size());
            if (verbose) (new Throwable("sorted = true")).printStackTrace(System.err);
         }
    The comparator knows how to sort on any of three fields, depending on an enum
    value which my app can set based n a popup menu selection.

    When I call the sort method with one field the list is sorted correctly and a trace
    in my getListCellRendererComponent is called for each line displayed by the list.
    So fat, so good.

    If my app then changes the comparator's sort field and invokes the model's sort
    again, traces inside the model show correct sorting but the JList displays n
    copies of a single record. The record displayed seems to always be two records
    below the one selected when sort was called (unless the selected record is the
    next-to-last in the list when the last one is replicated).

    Even stranger, when the list data gets trashed getListCellRendererComponent doesn't seem to be invoked at all; but subsequently sorting on the frst choice of fields, the
    getListCellRendererComponent for each displayed record and everything appears
    correctly again.

    I'm hoping someone can suggest a place fr me to look to find the culprit. My app
    is just shy of 10K lines so I really can't post the whole thing. Any ideas will be
    appreciated... especially if they point me at the solution!

    If anyone cares, I'm running JDK 1.8.9_91 on a Windoze 7 PC.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Strange JList Problem

    I really can't post the whole thing.
    Yes, 10K lines are too many.
    Try to strip it down to a sample for testing with < 300 lines. Post just what's needed to show the problem. Something that will compile, execute and show the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Strange JList Problem

    Not certain if I can offer much. But it sounds like it might be tied to the cell renderer. Would it be possible to use the default renderer and have your data displayed in a more generic fashion. The purpose of that would be to see if the sorted data is then displayed in some way it could be verified outside the model. When I have worked with cell renderers and models before I know that they can give the impression that the data is messed up whereas it the objects are just be displayed incorrectly (or the wrong objects are being displayed). The other possibility would be that a reference or references are not being updated correctly when you change sorting modes.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default Re: Strange JList Problem

    Thanks jim829. Good guess! I found the coder malfunction in the renderer.

Similar Threads

  1. Strange problem
    By djokovic in forum AWT / Swing
    Replies: 5
    Last Post: 07-07-2012, 12:20 PM
  2. Strange font problem
    By BayPhilip in forum Jobs Discussion
    Replies: 0
    Last Post: 03-01-2011, 10:11 PM
  3. strange problem
    By dinosoep in forum New To Java
    Replies: 6
    Last Post: 05-01-2010, 10:28 AM
  4. Strange Problem
    By Aseem in forum JDBC
    Replies: 5
    Last Post: 02-12-2010, 07:46 AM
  5. Strange problem
    By Tamir in forum Eclipse
    Replies: 1
    Last Post: 08-18-2008, 09:44 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
  •