JTable Single Cell renderer

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
conorocallaghanAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
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
 
objectsCommented:
0
 
zzynxConnect With a Mentor Software engineerCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
zzynxSoftware engineerCommented:
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
 
CEHJCommented:
>>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
 
zzynxSoftware engineerCommented:
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
 
conorocallaghanAuthor Commented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
>> 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
 
conorocallaghanAuthor Commented:
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
 
CEHJCommented:
>> 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
 
zzynxSoftware engineerCommented:
>> 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
 
conorocallaghanAuthor Commented:
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
 
conorocallaghanAuthor Commented:
Agreed!
0
 
CEHJCommented:
>>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
 
CEHJCommented:
>>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
 
conorocallaghanAuthor Commented:
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
 
zzynxSoftware engineerCommented:
>> Agreed!
OK, I'll ask.
0
 
zzynxSoftware engineerCommented:
>> 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
 
conorocallaghanAuthor Commented:
Everyone happy now?
0
 
zzynxSoftware engineerCommented:
>> 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
 
CEHJCommented:
>>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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.