Solved

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

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 65
CSV file parsing thru Java 13 40
Java List 4 41
activeMQ Queue Messages Enqueued and Messages Dequeued 1 39
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

726 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