Solved

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

Posted on 2003-12-05
5
309 Views
Last Modified: 2012-05-04
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
Comment
Question by:rvaldivia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9883400
These are really two quite separate questions

a. you need to sort the TableModel
b. you need to set the cell renderer appropriately
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9883413
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9883444
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question