[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JTable Single Cell renderer

Posted on 2005-05-10
24
Medium Priority
?
416 Views
Last Modified: 2008-01-09
I've a JTable with 1 column and 200,000+ rows..
When the user clicks on a certain component I calculate which rows I wish to color differently..
So can someone explain how I could color specific cells when given their row number and column number.
Sample code would be very helpful.
Thanks in advance
0
Comment
Question by:conorocallaghan
  • 9
  • 6
  • 6
  • +1
22 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 13966659
See

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer

you just return the component set with the correct colour based on the row and column of the following:

public Component getTableCellRendererComponent(
                            JTable table, Object o,
                            boolean isSelected, boolean hasFocus,
                            int row, int column)
                           
                           
0
 
LVL 92

Expert Comment

by:objects
ID: 13966670
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 500 total points
ID: 13966711
Example of a renderer (that makes the background of the even rows green)

public class MyColorTableCellRenderer extends DefaultTableCellRenderer {

    public MyColorTableCellRenderer() {
    }
   
    public Component getTableCellRendererComponent(
                                JTable table, Object value, boolean isSelected,
                                boolean hasFocus, int row, int column) {
        if (row % 2 = 0) // makes the background color of the (unselected) even rows green
            setBackground( isSelected ? table.getSelectionBackground() : Color.GREEN );
        else
            setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
        setForeground( isSelected ? table.getSelectionForeground() : table.getForeground()) ;
        return this;
    }
}

Apply it to your table:

yourTable.setDefaultRenderer(new MyColorTableCellRenderer());
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 13966734
Of course you can also
- change the foreground
- relate the color changes to the column number or to both the row and column number

e.g. The same as above, but the 1st column (col=0) of the 10th row (row=9) will
       always - selected or not - have a red background:

public class MyColorTableCellRenderer extends DefaultTableCellRenderer {

    public MyColorTableCellRenderer() {
    }
   
    public Component getTableCellRendererComponent(
                                JTable table, Object value, boolean isSelected,
                                boolean hasFocus, int row, int column) {
        if (row==9 && col==0)
            setBackground( Color.RED );
        if (row % 2 = 0) // makes the background color of the (unselected) even rows green
            setBackground( isSelected ? table.getSelectionBackground() : Color.GREEN );
        else
            setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
        setForeground( isSelected ? table.getSelectionForeground() : table.getForeground()) ;
        return this;
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13967449
>>Sample code would be very helpful.

Sample code *is* helpful but it's not a reason to ignore previous correct answers. Please see the guidelines conorocallaghan
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13967517
Thanks for accepting.

For my interest: Could you tell me what you missed in my comment for not awarding it with an A?
For your interest: You can always ask for more explanation if you're not completely satisfied with a comment.
0
 

Author Comment

by:conorocallaghan
ID: 13967575
I'm sorry, but to be honest I need to look at the guidelines for these forums just like CEHJ said. You did indeed answer my question however did not completely fix my problem.. I will take more care rewarding in the future.
Apolagies :--(
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13967600
>> however did not completely fix my problem..
Then why didn't you ask for a more specific answer (until your problem is completely fixed)
instead of closing this question?

>> Apolagies
Accepted :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13967611
>> fix my problem
Moreover, what problem are you referring to?
Wasn't this your Q:
>> can someone explain how I could color specific cells when given their row number and column number
0
 

Author Comment

by:conorocallaghan
ID: 13967657
Indeed you did actualy answer my question how to "color specific cells when given their row number and column number"
In fact it was exactly what I needed, however it did not totaly fix a problem that I had.  You know how it is, when you get caught up coding, you can tend to lose sight of other things...
 Sorry again, I've learnt my lesson :-/


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13967671
>> Indeed you did actualy answer my question how to "color specific cells when given their row number and column number"

Indeed i did. I'm just wondering why i received no points at all ...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13967681
>> In fact it was exactly what I needed
>> however it did not totaly fix a problem that I had
But that you didn't tell us
>> I will take more care rewarding in the future.
Well, if you agree I can ask a moderator to change the grade from B to A.
0
 

Author Comment

by:conorocallaghan
ID: 13967704
CEHJ I was actualy talking ot zzynx, his sample code helped me understand things better, in fact it helped me solve the problem. However I do agree that  I must take more care in marking in the future.

Please accept my apology....
0
 

Author Comment

by:conorocallaghan
ID: 13967707
Agreed!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13967710
>>Well, if you agree I can ask a moderator to change the grade from B to A.

And while the moderator is about it, perhaps you can award me points for answering your question too
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13967724
>>his sample code helped me understand things better, in fact it helped me solve the problem

I'm well aware of that, but it doesn't mean previous correct answers should be totally ignored
0
 

Author Comment

by:conorocallaghan
ID: 13967731
Also I would like to possibly split the the marks between both zzynx and CEHJ as both people gave valid answers, while I found zzynx's answer more helpful
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13967742
>> Agreed!
OK, I'll ask.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13967760
>> Also I would like to possibly split the the marks between both zzynx and CEHJ
I'll ask to reopen this Q.
>> ... while I found zzynx's answer more helpful
Once reopened you can split the points the way you want and re-accept.
0
 

Author Comment

by:conorocallaghan
ID: 13967773
Everyone happy now?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13967787
>> I'll ask to reopen this Q.
Done. (http://www.experts-exchange.com/Community_Support/Q_21418809.html)

PS. CEHJ, I don't say your comment wasn't correct, but it could have been more detailed. ;°)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13971108
>>PS. CEHJ, I don't say your comment wasn't correct, but it could have been more detailed. ;°)

Comments can nearly always be more detailed ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 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