Solved

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

Posted on 2003-12-05
5
275 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
Receive file in Servlet 1 36
sumDigits  challenge 7 60
Exception after setting jdbc session management 2 38
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now