Results 1 to 7 of 7

Thread: JTable sorting

  1. #1
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Smile JTable sorting

    in my code, jtable sorting done by TableRowSorter...

    is it possible to sort table... and ... let say... last row of table remain at the bottom of table after clicking the table Header(sort the table value of row in asc/desc)

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    You can create a comparator to do the specific sort you want. Then use the tableRowSorter.setComparator method to set the comparator for the specific column.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    is that possible to keep the last row at the bottom of table, no matter what its value and in the sorting in asc or desc order

    so...
    how do compare(o1, o2) know the object is in the last row?

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Well, I had two ideas, and neither seem to really work well. Perhaps I'm not familiar enough with JTable, so I'll state my ideas and hopefully they can give some inspiration.

    The first idea was to implement a sort method directly, but exclude the last row. This idea could be extended to exclude any row(s). Since you can modify the table's entries, you should be able to write an external sort method, and then fill the values in. However, I don't have enough swing experience to know how to do this.

    The second idea was to create a wrapper for an object (a NoSort class). Then, in the comparator, if the object is the NoSort object, leave the two values in the order they are. This method only works if there's one NoSort object.

    For example, if row 1 is sortable, 2 is not, 3 is, and 4 is not. When using a merge sort, first row 1 and row 2 would be sorted. However, since row 2 is not sortable, it remains as is (thus row 1 remains). Similarly, row 3 and 4 remain as is, since row 4 is not sortable. In other words, this order is maintained regardless if the sort is in ascending or descending order. Note that row 1 and row 3 are sortable, and should be sorted (in either ascending or descending order).


    The code below demonstrates the "goal" of the second idea, but the implementation is poor. However, I provide it, hoping that you can make use of it.

    Java Code:
    import java.util.Comparator;
    
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
    
    public class Test
    {
    	/**
    	 * Main method
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		TableModel tableModel = new DefaultTableModel(4, 1);
    		TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
    		final JTable table = new JTable(tableModel);
    		table.setRowSorter(sorter);
    						
    		final Comparator<Object> ascendingColumn0 = new Comparator<Object>() {
    			
    			@Override
    			public int compare(Object o1, Object o2)
    			{
    				if (o1 instanceof NoSort<?> || o2 instanceof NoSort<?>)
    				{
    					//maintain order
    					return -1;
    				}
    				
    				Integer int1 = (Integer) o1;
    				Integer int2 = (Integer) o2;
    				
    				return int1.compareTo(int2);
    			}
    		};
    		
    		final Comparator<Object> decendingColumn0 = new Comparator<Object>() {
    			@Override
    			public int compare(Object o1, Object o2)
    			{
    				return ascendingColumn0.compare(o2, o1);
    			}
    		};
    		
    		table.setValueAt(1, 0, 0);
    		table.setValueAt(3, 1, 0);		
    		table.setValueAt(7, 2, 0);
    		table.setValueAt(new NoSort<Integer>(21), 3, 0);
    		
    		System.out.println("Ascending order:");
    		sorter.setComparator(0, ascendingColumn0);
    		sorter.toggleSortOrder(0);
    		output(table);
    		
    		System.out.println("Descending order:");
    		sorter.setComparator(0, decendingColumn0);
    		sorter.sort();
    		output(table);		
    	}
    	
    	private static void output(JTable table)
    	{
    		for (int i = 0; i < table.getRowCount(); i++)
    		{
    			System.out.println(table.getValueAt(i, 0));
    		}
    	}
    	
    	/**
    	 * Wrapper class for an object you don't want sorted
    	 */
    	static class NoSort<T>
    	{
    		T value;
    		
    		NoSort(T value)
    		{
    			this.value = value;
    		}
    		
    		public T getValue()
    		{
    			return value;
    		}
    		
    		public void setValue(T value)
    		{
    			this.value = value;
    		}
    		
    		@Override
    		public String toString()
    		{
    			return value.toString();
    		}
    	}
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i change the NoSort to implements interface Comparable, with method compareTo(T)
    add instance variable sort, which indicate the NoSort object should sort or not
    Java Code:
    	public int compareTo(T o)
    	{
    		NoSort<?> tmpObj = (NoSort<?>)o;
    		if (this.sort == false || tmpObj.sort == false)
    		{
    			//maintain order
    			return 0;
    		}
    		else
    		{
    			return ((Comparable) this.value).compareTo(tmpObj.value);
    		}
    	}

    i also override getColumnClass, getValueAt, setValueAt of DefaultTableModel

    now the table support NoSort to Number and String


    thx very much CodesAway.

  7. #7
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    So you got it working? Awesome! Can you post the updated code (I'm curious how it looks)? Is it able to handle multiple non-sorting values, or just one?

    From your code, I see that 0 (and not -1) is what I should have used to maintain order. Changing that single line allows using sorter.toggleSortOrder(0); to toggle between ascending and descending order. Also, only one comparator is required (no need to switch comparators when changing the sort order). I'm REALLY glad I posted my idea, I learned something really useful! I completely forgot that since the sort is stable, returning 0 would keep the values in order. So, thank you very much for that!
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Sorting in JTable without clicking on column headers
    By ProgrammingPup in forum Advanced Java
    Replies: 6
    Last Post: 11-04-2009, 05:26 AM
  2. Replies: 7
    Last Post: 09-23-2009, 07:28 PM
  3. Sorting JTable (Vectors) Problem
    By ramapple in forum AWT / Swing
    Replies: 6
    Last Post: 07-06-2009, 11:15 PM
  4. Sorting JTable
    By mm2236 in forum AWT / Swing
    Replies: 0
    Last Post: 04-09-2009, 04:37 PM
  5. sorting JTable
    By mansi_3001 in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 06:29 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
  •