Results 1 to 3 of 3

Thread: JList Sorting

  1. #1
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Question JList Sorting

    Hello All,

    Can you please guide me how can I sort the contents of a JList? I am using the DefaultListModel. Items will be added to the JList dynamically like this :

    Class c1= new Class (parameter1,parameter2, parameter3);
    listModel.addElement(c1);

    Now I would like to sort it like (parameter2,parameter1,parameter3)

    Can you please guide me?


    Thanks all in advance.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    If you are adding an Object with 3 values to the list, then you should be using a JTable, not a JList. A JTable is designed to display multiple columns. A JList is designed to display only a single column. JTable provides built in support for sorting.

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default How to sort a DefaultListModel

    In this reply I assume that "sort it like (parameter2,parameter1,parameter3)"
    means to reorder the elements of the model in some way.
    No matter how you want to define the ordering, you only need to write a method
    to compare two elements and decide which should come before the other.

    First you make the Class have the Comparable interface
    and define the comparison method:
    Java Code:
    class Class implements Comparable<Class> {
        ...
        // the compareTo method is defined by the Comparable interface
        int compareTo(Class o) {
            if (  [I][COLOR="DarkOrchid"]this comes before o[/COLOR][/I]  ) return -1;
            else if (   [COLOR="DarkOrchid"]this comes after o[/COLOR]    ) return +1;
            return 0;  // it is best if this case only happens when this == o
        }
        ...
    }  // end class Class

    Then actual sorting can be done with a method like this
    Java Code:
    static void sort(DefaultListModel dlm) { 
        Class [] dlma = dlm.toArray();    // make an array of the elements in the model
        Arrays.sort(dlma);   // sort the array (this step uses the compareTo method)
        dlm.clear();     // empty the model
        for (Class x : dlma)
            dlm.add(x);       // insert all the elements into the model in sorted order
    }

Similar Threads

  1. How to use a JList?
    By glhansen in forum New To Java
    Replies: 3
    Last Post: 03-24-2009, 10:27 AM
  2. JList
    By pinks_70986 in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 08:36 AM
  3. About JList
    By hungleon88 in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2008, 09:24 PM
  4. Help with JList
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 03:42 PM
  5. add a jlist column
    By Alan in forum JCreator
    Replies: 1
    Last Post: 05-28-2007, 04:51 AM

Posting Permissions

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