« Return to Thread: EventListJXTableSorting

Re: EventListJXTableSorting

by James Lemieux :: Rate this Message:

Reply to Author | View in Thread

Steve,

1. Your test class fails to work reliably because you're not building / showing your Swing components on the EDT. Wrap everything in your main method in a Runnable and post it to the EDT like so:

SwingUtilities.invokeLater (new Runnable() {
   public void run() {
      GlazedListJXTableProblem frame = null;

      if( args.length > 0 ) {
         frame = new GlazedListJXTableProblem( args[ 0 ] );
      } else {
         System.out.println( "specify List as first parameter: { JTable | JXTableJXSorting | JXTableGLSorting }");
         System.exit( 0 );
      }

      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.setVisible( true );
   }
});


2. I read the class doc for EventListJXTableSorting and it explains how to use it:

a) Create a {@link SortedList} and {@link EventTableModel} that depends on that {@link SortedList}.
b) Create a {@link JXTable} using the {@link EventTableModel} as its model.
c) Run the {@link EventListJXTableSorting#install} method to bind the {@link JXTable}'s headers to the {@link SortedList}'s {@link Comparator}.

Notice that it says nothing about TableComparatorChooser. In fact, later in the class doc it even mentions this:

"The behaviour of multiple column sorting in JXTable is not particularly flexible. For different configuration options, consider using a {@link TableComparatorChooser} instead of {@link EventListJXTableSorting}."


3. Removing the call to TableComparatorChooser.install seems to produce the behaviour you are looking for.


4. I was using the latest jar of GL and the latest jar of swingx and I never saw two sort icons like the snapshots you pointed at. That said, I'd guess they are caused by having two different table sorting mechanisms in play (TableComparatorChooser and EventListJXTableSorting).

Hope this helps,

James

On 7/30/07, Steve Newell <newellista@...> wrote:

I have created a test case that demonstrates the problems I am having.  To
summarize:
1. JTable works exactly as I would expect.
2. JXTable without EventListJXTableSorting does the following:
    a. If I don't set a breakpoint on the pack() call in the constructor,
the program hangs.  It seems to hang attempting to get a ReadLock.  Here is
the stack trace:
http://www.nabble.com/file/p11865381/Picture%2B6.png
    b. If I do set the breakpoint on the pack() call, and run past that,
things seem to work, except when you click on the column header to sort a
column, I get two up/down arrows.
http://www.nabble.com/file/p11865381/Picture%2B8.png
3. JXTable with EventListJXTableSorting does the following:
    a. Same problem with the readlock as described above
    b. List is not in sorted order initially, even though I specify a
Comparator in the SortedList constructor
    c. Clicking on the column header does cause the sort to occur, but there
are two up/down arrows in the header, as above, but one is pointing up, and
the other is pointing down.
http://www.nabble.com/file/p11865381/Picture%2B7.png

Here is the source that demonstrates the problem.  I am using
glazedlists_java1.5.jar, the latest swingx.jar.  I am running on Mac OS X
with Java 1.5.0_07.

If I can do anything more to help debug/isolate the problem, let me know.

Thanks again for all your help.

Steve

package problem;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Comparator;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import org.jdesktop.swingx.decorator.RolloverHighlighter ;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.gui.AbstractTableComparatorChooser;
import ca.odell.glazedlists.gui.TableFormat ;
import ca.odell.glazedlists.swing.EventListJXTableSorting;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TableComparatorChooser;

public class GlazedListJXTableProblem extends JFrame
{
        public GlazedListJXTableProblem()
        {
                initialize( "JTable" );
        }

        public GlazedListJXTableProblem( String whichList )
        {
                initialize( whichList );
        }

        private void initialize( String whichList )
        {
                JPanel panel = new JPanel( new BorderLayout() );
                panel.add( getList( whichList ), BorderLayout.CENTER );
                getContentPane().setLayout( new BorderLayout() );
                getContentPane().add( panel, BorderLayout.CENTER );
                pack();
        }

        private JScrollPane getList( String whichList )
        {
                if( whichList.equals( "JTable" ) )
                {
                        return getJTable();
                }
                else if( whichList.equals( "JXTableGLSorting" ) )
                {
                        return getJXTableGLSorting();
                }
                else if( whichList.equals( "JXTableJXSorting" ) )
                {
                        return getJXTableJXSorting();
                }

                return null;
        }

        private JScrollPane getJXTableGLSorting()
        {
                JScrollPane scrollImageTable = null;
                EventList< ImageSignature > imageList = new BasicEventList< ImageSignature
>();

                SortedList< ImageSignature > sortedList = new SortedList< ImageSignature
>( imageList, new ImageSizeComparator() );

                EventTableModel< ImageSignature > tableModel = new EventTableModel<
ImageSignature >( sortedList, new ImageTableFormat() );

                JXTable imageTable = new JXTable( tableModel );

                TableComparatorChooser.install( imageTable, sortedList,
AbstractTableComparatorChooser.SINGLE_COLUMN );

                scrollImageTable = new JScrollPane( imageTable );
                imageTable.setHighlighters( HighlighterFactory.createAlternateStriping(),
new RolloverHighlighter( Color.BLACK, Color.white ) );
                imageTable.setRolloverEnabled( true );
                imageTable.setHorizontalScrollEnabled( true );

                imageTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
                imageTable.setColumnControlVisible( true );

                insertTestData( imageList );

                return scrollImageTable;
        }
        private JScrollPane getJTable()
        {
                JScrollPane scrollImageTable = null;
                EventList< ImageSignature > imageList = new BasicEventList< ImageSignature
>();

                SortedList< ImageSignature > sortedList = new SortedList< ImageSignature
>( imageList, new ImageSizeComparator() );

                EventTableModel< ImageSignature > tableModel = new EventTableModel<
ImageSignature >( sortedList, new ImageTableFormat() );

                JTable imageTable = new JTable( tableModel );

                TableComparatorChooser.install( imageTable, sortedList,
AbstractTableComparatorChooser.SINGLE_COLUMN );

                scrollImageTable = new JScrollPane( imageTable );

                 imageTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

                insertTestData( imageList );

                return scrollImageTable;
        }

        private JScrollPane getJXTableJXSorting()
        {
                JScrollPane scrollImageTable = null;
                EventList< ImageSignature > imageList = new BasicEventList< ImageSignature
>();

                SortedList< ImageSignature > sortedList = new SortedList< ImageSignature
>( imageList, new ImageSizeComparator() );

                EventTableModel< ImageSignature > tableModel = new EventTableModel<
ImageSignature >( sortedList, new ImageTableFormat() );

                JXTable imageTable = new JXTable( tableModel );

                TableComparatorChooser.install( imageTable, sortedList,
AbstractTableComparatorChooser.SINGLE_COLUMN );

                EventListJXTableSorting sorting = EventListJXTableSorting.install(
imageTable, sortedList );
                sorting.setMultipleColumnSort( false );

                scrollImageTable = new JScrollPane( imageTable );
                imageTable.setHighlighters( HighlighterFactory.createAlternateStriping (),
new RolloverHighlighter( Color.BLACK, Color.white ) );
                imageTable.setRolloverEnabled( true );
                imageTable.setHorizontalScrollEnabled( true );

                imageTable.setSelectionMode ( ListSelectionModel.SINGLE_SELECTION );
                imageTable.setColumnControlVisible( true );

                insertTestData( imageList );

                return scrollImageTable;
        }

        private void insertTestData(EventList< ImageSignature > imageList  )
        {
                Random dice = new Random( System.currentTimeMillis() );
                imageList.getReadWriteLock().writeLock().lock();
                try
                {
                        for( int i = 0; i < 100; i++ )
                        {
                                ImageSignature is = new ImageSignature();
                                 is.setDirectory( "Directory" + ( i % 10 ) );
                                is.setFilename( "filename" + i );
                                is.setFilesize( Math.abs( dice.nextInt() ) );
                                 imageList.add( is );
                        }
                }
                finally
                {
                        imageList.getReadWriteLock().writeLock().unlock();
                }
        }
        class ImageSizeComparator implements Comparator< ImageSignature >
        {
                public int compare( ImageSignature sig1, ImageSignature sig2 )
                {
                        return ( int ) ( sig2.getFilesize() - sig1.getFilesize() );
                }
        }

        public static void main( String[] args )
        {
                GlazedListJXTableProblem frame = null;

                if( args.length > 0 )
                {
                        frame = new GlazedListJXTableProblem( args[ 0 ] );
                }
                else
                {
                        System.out.println ( "specify List as first parameter: { JTable |
JXTableJXSorting | JXTableGLSorting }");
                        System.exit( 0 );
                }

                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setVisible( true );
        }

        protected static class ImageSignature
        {
                private String directory;
                private String filename;
                private long fileSize;

                public String getDirectory()
                {
                        return directory;
                }
                public void setDirectory( String directory )
                {
                        this.directory = directory;
                }
                public String getFilename()
                {
                        return filename;
                }
                public void setFilename( String filename )
                {
                        this.filename = filename;
                }
                public long getFilesize()
                {
                        return fileSize;
                }
                public void setFilesize( long fileSize )
                {
                        this.fileSize = fileSize;
                }
        }
        protected static class ImageTableFormat implements TableFormat<
ImageSignature >
        {
                public static final int DIRECTORY = 0;
                public  static final int FILENAME = 1;
                public  static final int SIZE = 2;

                protected String[] headers;

                public ImageTableFormat()
                {
                        headers = new String[] { "Directory", "Filename", "Size" };
                }

                public int getColumnCount()
                {
                        return 3;
                }

                public String getColumnName( int col )
                {
                        return headers[ col ];
                }

                public Object getColumnValue( ImageSignature imageSignature, int col )
                {
                        switch( col )
                        {
                                case FILENAME:
                                        return imageSignature.getFilename();
                                case SIZE:
                                        return imageSignature.getFilesize();
                                case DIRECTORY:
                                        return imageSignature.getDirectory();
                        }
                        return null;
                }
        }
}



1. A SortedList does not impose an order on its source EventList until it
has been given a Comparator. A Comparator can either be given in the
constructor or given using setComparator(). So if you are passing a
Comparator-less SortedList into your EventTableModel then NO sorting will
occur.

2. Jesse needs to weigh in on EventListJXTableSorting since he authored it.

3. If your FilterList is not behaving correctly then either
   a) your Matcher logic isn't doing the right thing
   b) your MatcherEditor (if any) is firing the wrong kind of
MatcherEditor.Event because you are calling the fire method. i.e. make sure
you're calling fireConstrained, fireRelaxed, etc at the appropriate times.

4. The best way for me to make headway on your EventListJXTableSorting bug
is for you to produce a test class that demonstrates it. I can promise a
speedy resolution if you can do that.

James


--
View this message in context: http://www.nabble.com/EventListJXTableSorting-tf4148508.html#a11865381
Sent from the GlazedLists - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


 « Return to Thread: EventListJXTableSorting