How to synchronize to JXTables?

View: New views
8 Messages — Rating Filter:   Alert me  

How to synchronize to JXTables?

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
i have got to JXTables using the same datamodel. When i do a sort in one of the tables the otherone has to get sorted, too. Does anyone have an idea how to synchronize the indices of the rows of two tables?
thx
[Message sent by forum member 'doener' (doener)]

http://forums.java.net/jive/thread.jspa?messageID=286935

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


Re: How to synchronize to JXTables?

by Noel Grandin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Sounds like you need one master TableModel, linked to 2 JTables via
translator TableModels,
where the job of the translator tablemodel is to expose different sets
of columns to the 2 different tables.

Regards, Noel.

jdnc-interest@... wrote:

> Hi,
> i have got to JXTables using the same datamodel. When i do a sort in one of the tables the otherone has to get sorted, too. Does anyone have an idea how to synchronize the indices of the rows of two tables?
> thx
> [Message sent by forum member 'doener' (doener)]
>
> http://forums.java.net/jive/thread.jspa?messageID=286935
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@...
> For additional commands, e-mail: jdnc-help@...
>
>
>  


Disclaimer: http://www.peralex.com/disclaimer.html



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


Re: How to synchronize to JXTables?

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thats what i have got and it works perfect except sorting. It seems like only the indices of the rows change when i use the standard sorting of JXTable. The data in the model is never touched. When i sort the data of the tablemodel both tables get updated, but that is not a nice way to do this.
[Message sent by forum member 'doener' (doener)]

http://forums.java.net/jive/thread.jspa?messageID=286945

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


Re: How to synchronize to JXTables?

by Kleopatra :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

jdnc-interest@... schrieb:
> Thats what i have got and it works perfect except sorting. It seems like only the indices of the rows change when i use the standard sorting of JXTable. The data in the model is never touched. When i sort the data of the tablemodel both tables get updated, but that is not a nice way to do this.
>  

One of the shortcomings of SwingX sorting/filtering is that it is
strictly per-table-instance. There is no easy way to synch several
tables. Could be done, though:, I remember that we discussed it, maybe
the incubator has example (could be in paultaylor and/or kleopatra).
Searching the forum might help as well.

Sorry for sparse help (I'm not here ;-)
Jeanette

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


Re: How to synchronize to JXTables?

by Noel Grandin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Aaah, I see.

Expect I don't understand why you're using 2 tables? If they are so
closely linked, then surely you should be using one table?
I think you're trying to solve the problem at the wrong level.
If you have 2 sets of columns that you want to look distinct, maybe you
should be using a "fake" column to draw a solid block between them?

Otherwise, if you really want to do this, you could try starting with
the original TableSorter code from here:
 
http://java.sun.com/docs/books/tutorial/uiswing/components/examples/TableSorter.java
But it'll require a fair amount of work to get to where you want to be.

jdnc-interest@... wrote:

> Thats what i have got and it works perfect except sorting. It seems like only the indices of the rows change when i use the standard sorting of JXTable. The data in the model is never touched. When i sort the data of the tablemodel both tables get updated, but that is not a nice way to do this.
> [Message sent by forum member 'doener' (doener)]
>
> http://forums.java.net/jive/thread.jspa?messageID=286945
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@...
> For additional commands, e-mail: jdnc-help@...
>
>
>  


Disclaimer: http://www.peralex.com/disclaimer.html



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


Re: How to synchronize to JXTables?

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The reason for two tables is that i need one table with a fixed left column. Here is an example for this:

http://www.codeguru.com/java/articles/128.shtml
[Message sent by forum member 'doener' (doener)]

http://forums.java.net/jive/thread.jspa?messageID=287053

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


Re: How to synchronize to JXTables?

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Not sure how "correct" it is, in terms of correct SwingX approaches, but working with paultaylor a while ago we came up with the following multi-sort class:

[code]
import java.util.EventListener;
import java.util.EventObject;

import javax.swing.event.EventListenerList;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.FilterPipeline;
import org.jdesktop.swingx.decorator.SortOrder;
import org.jdesktop.swingx.table.TableColumnExt;

import uk.co.crowderconsult.filters.SyncSorterFilterPipeline;


public class MultipleTableSort {
         public static class SortEvent extends EventObject
            {
                private int[] newOrder;
         
                /**
                 * Constructs a SortEvent object.
                 *
                 * @param source   the source Object (typically <code>this</code>)
                 * @param newOrder of records
                 */
                public SortEvent(Object source, int[] newOrder)
                {
                    super(source);
                    this.newOrder = newOrder;
                }
         
                /**
                 * Returns a string representation of this event
                 *
                 * @return a string representation of this event.
                 */
                public String toString()
                {
                    return getClass().getName();
                }
         
                public int[] getNewOrder()
                {
                    return newOrder;
                }
            }
           
            public static class ResetEvent extends EventObject
            {
               
         
                /**
                 * Constructs a ResetEvent object.
                 *
                 * @param source   the source Object (typically <code>this</code>)
                 
                 */
                public ResetEvent(Object source)
                {
                    super(source);
                   
                }
         
                /**
                 * Returns a string representation of this event
                 *
                 * @return a string representation of this event.
                 */
                public String toString()
                {
                    return getClass().getName();
                }
         
               
            }
         
            public static interface SortListener extends EventListener
            {
                public void sortReceived(SortEvent event);
            }
           
            public static interface ResetListener extends EventListener
            {
                public void resetReceived(ResetEvent event);
            }
         
            public static class SyncTable extends JXTable  implements SortListener, ResetListener
            {
               
            public SyncTable(TableModel dm)
                {
                    super(dm);
                    FilterPipeline pipeline = new SyncSorterFilterPipeline();
                    setFilters(pipeline);
                }
         
                public SyncTable() {
                                super();
                                FilterPipeline pipeline = new SyncSorterFilterPipeline();
                    setFilters(pipeline);
                        }

                        public void sortReceived(SortEvent e)
                {
                    if (e.getSource() != this)
                    {
//                ((SyncTable)e.getSource()).resetMainSortedColumn();
                    sortToMatchTable(e);
                    }
                }
         
         
                EventListenerList listenerList = new EventListenerList();
         
         
                public synchronized void addSortListener(SortListener l)
                {
                    listenerList.add(SortListener.class, l);
                }
               
                public synchronized void addResetListener(ResetListener l)
                {
                listenerList.add(ResetListener.class, l);
                }
         
                public synchronized void removeSortListener(SortListener l)
                {
                    listenerList.remove(SortListener.class, l);
                }
               
                public synchronized void removeResetListener(ResetListener l)
                {
                    listenerList.remove(ResetListener.class, l);
                }
         
                public synchronized void fireSortEvent(int[] newOrder, Object Source)
                {
                    this.setMainSortedColumn(this.getSortedColumn());
                SortEvent sortEvent = null;
                    Object[] listeners = listenerList.getListenerList();
         
                    for (int i = listeners.length - 2; i >= 0; i -= 2)
                    {
                        if (listeners[i] == SortListener.class)
                        {
                            // Lazily create the event:
                            if (sortEvent == null)
                            {
                                sortEvent = new SortEvent(this, newOrder);
                            }
                            ((SortListener) listeners[i + 1]).sortReceived(sortEvent);
                        }
                    }
                }
               
                public synchronized void fireResetEvent(Object Source)
                {
                    ResetEvent resetEvent = null;
                    Object[] listeners = listenerList.getListenerList();
         
                    for (int i = listeners.length - 2; i >= 0; i -= 2)
                    {
                        if (listeners[i] == ResetListener.class)
                        {
                            // Lazily create the event:
                            if (resetEvent == null)
                            {
                                resetEvent = new ResetEvent(this);
                            }
                            ((ResetListener) listeners[i + 1]).resetReceived(resetEvent);
                        }
                    }
                }
         
                public boolean isSortable(int columnIndex)
                {
                return super.isSortable(columnIndex);
                }
                @Override
                public void toggleSortOrder(int columnIndex)
                {
                  if (columnIndex < getColumnCount())
                  {
                if (!isSortable(columnIndex))
                    {
                System.out.println("Tried to sort a non-sortable column");
                        return;
                    }
                    SyncSorterFilterPipeline.CustomSortController controller = (SyncSorterFilterPipeline.CustomSortController) getSortController();
                    TableColumnExt columnExt = getColumnExt(columnIndex);
         
                    int[] newOrder = controller.performSortOrder(convertColumnIndexToModel(columnIndex),
                        columnExt != null ? columnExt.getComparator() : null);
                    currentSortOrder = controller.getSortOrder(columnExt.getModelIndex());
         
                    fireSortEvent(newOrder, this);
                  }
                }
         
                @Override
                public void resetSortOrder()
                {
                if (mainSortedColumn != null)
                {
                super.resetSortOrder();
                this.currentSortOrder = null;
               
                fireResetEvent(this);
                }
                }
         
                public void sortToMatchTable(SortEvent event)
                {
//            clearSelection();
                if (getSortedColumn() != null)
                    {
                        super.resetSortOrder();
                       this.resetMainSortedColumn();
                    }
         
                    /* Sort */
                    TableColumnExt columnExt = getColumnExt("synccol");
                    if (getSortController() instanceof SyncSorterFilterPipeline.CustomSortController)
                    {
                    SyncSorterFilterPipeline.CustomSortController controller
                        = (SyncSorterFilterPipeline.CustomSortController) getSortController();
                   
                    controller.syncSortOrder(columnExt.getModelIndex(),
                        columnExt != null ? columnExt.getComparator() : null,
                        event.getNewOrder());
                    }
                   
                   
         
                }

                public void setMainSortedColumn(TableColumn sorted)
                {
                mainSortedColumn = sorted;
                }
               
                public TableColumn getMainSortedColumn()
                {
                return mainSortedColumn;
                }
               
                public void resetMainSortedColumn()
                {
                mainSortedColumn = null;
                }
               
                        public void resetReceived(ResetEvent event) {
                               
                                 this.resetMainSortedColumn();
                                if (event.getSource() != this)
                                {
                                        super.resetSortOrder();
                                       
                                        //clearSelection();
                                       
                                }
                               
                        }
                       
                        public SortOrder getCurrentSortOrder()
                        {
                                return currentSortOrder;
                        }
                        public void resetCurrentSortOrder()
                        {
                                currentSortOrder = null;
                        }
                        TableColumn mainSortedColumn;
                        SortOrder currentSortOrder;
            }
         
           
         
}
[/code]


Then with the tables (e.g. table1 and table2)  you wish to syncronise you'd have:

[code]

table1.addSortListener(table2);
table2.addSortListener(table1);

table1.addResetListener(table2);
table2.addResetListener(table1);

((TableColumnExt)table1.getColumnModel().getColumn(0)).setIdentifier("synccol");
((TableColumnExt)table2.getColumnModel().getColumn(0)).setIdentifier("synccol");
[/code]

Hope that helps, or at least gives you a starting point. Some of the more experienced people should be able to help if there are any obvious errors or bad practices in the above code.
[Message sent by forum member 'ruanae' (ruanae)]

http://forums.java.net/jive/thread.jspa?messageID=287086

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


Re: How to synchronize to JXTables?

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, i will give give it a try.
[Message sent by forum member 'doener' (doener)]

http://forums.java.net/jive/thread.jspa?messageID=287230

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

LightInTheBox - Buy quality products at wholesale price