• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Jtable: How to sort by columns and change the color of rows and columns

Hi, this is a quick question, I'm using a jtable and need to know how to sort by columns and change the color of rows and columns. I've been told that there is a propperty but I cannot find it.
Thanx in advance for your help.

Ron.
0
rvaldivia
Asked:
rvaldivia
  • 3
1 Solution
 
CEHJCommented:
These are really two quite separate questions

a. you need to sort the TableModel
b. you need to set the cell renderer appropriately
0
 
CEHJCommented:
a. Something like

class SortableDefaultTableModel extends DefaultTableModel implements Comparator {

      private int sortColumn;

      public void sortByColumn(int sortColumn){
            this.sortColumn = sortColumn;
            //System.out.println("Sorting on column " + sortColumn);
            Collections.sort(dataVector, this);
      }

      public int compare(Object o1, Object o2){
            if (o1 == null) return -1;
            if (o2 == null) return 1;

            Vector vect1 = (Vector) o1;
            Vector vect2 = (Vector) o2;
            Comparable col1 = (Comparable) vect1.get(sortColumn);
            Comparable col2 = (Comparable) vect2.get(sortColumn);
            return col1.compareTo(col2);
      }
}
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now