Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: JTable prepareRenderer changes all cell colors

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JTable prepareRenderer changes all cell colors

    Hi

    Im trying to change row colors in a JTable but the whole table changes to the same color. Only if I drag and highlight the whole table, the Specific rows im trying to change, show the correct color. I dont understand this, am I forgetting something?

    public vRows is a Vector set from the main class and mainTable is a customTable
    can I set vRows from the main Class?

    /************************************************** *********************/

    this is from the main Class in "button_clicked"

    Vector vColor = new Vector();
    vColor.add(1);
    vColor.add(2);
    vColor.add(6);
    vColor.add(7);


    mainTable.vRows = vColor;
    mainTable.setModel(model);
    mainTable.repaint();



    /************************************************** */
    public class CustomTable extends JTable {
    public Vector vRows;

    public CustomTable(int numRows, int numColumns) {
    super(numRows, numColumns);
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column){
    Component comp = super.prepareRenderer(renderer, row, column);

    int i = 0;
    while (vRows.size() > (i)){

    if (row == vRows.get(i)){
    comp.setBackground(Color.LIGHT_GRAY);

    }

    i++;
    }
    repaint();
    return comp;
    }
    /************************************************** ********************/



    Thanks
    Brett
    Last edited by BrettStuart; July 20th, 2010 at 08:56 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default Re: JTable prepareRenderer changes all cell colors

    Ok, I'm just brain-storming from the API here, so dont take everything I suggest to heart.

    Maybe you can take advantage of the setRowSelectionInterval(int index0, int index1) and the setSelectionBackground(Color selectionBackground) methods. To my best guess, you could go through the JTable, selecting rows with the setRowSelectionInterval method and set the color with the setSelctionBackground method.

    Try it, might work.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable prepareRenderer changes all cell colors

    Awesome thank you so much.
    It works perfectly.

    Brett

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default Re: JTable prepareRenderer changes all cell colors

    Good to know if I ever have to do it in the future.

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: JTable prepareRenderer changes all cell colors

    For what its worth, the reason you are receiving this behavior is because the renderer reuses the components. The solution is to reset the color in the prepareRenderer method

    if (row == vRows.get(i)){
        comp.setBackground(Color.LIGHT_GRAY);
    }else{
        comp.setBackground(Color.WHITE);//or whatever your default color is
    }

  6. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable prepareRenderer changes all cell colors

    Hi All

    Thanks for the help, now I have another problem.
    when I use the setRowSelectionInterval method the row colors are correct but when
    I use the getSelectedRow method to return the Row clicked on in the JTable it always returns the
    last row that I change the color of using setRowSelectionInterval method.

    So setRowSelectionInterval works to change colors of specific rows but returns the wrong row when calling getSelectedRow().

    I've also tried removing the RowSelectionInterval with removeRowSelectionInterval method, after I change the row colors but then the getSelectedRow returns -1

    Hi Copeg I have tried

    if (row == vRows.get(i)){
    comp.setBackground(Color.LIGHT_GRAY);
    }else{
    comp.setBackground(Color.WHITE);//or whatever your default color is
    }
    but this also only changes the color of the last row I change in the table eg. the last number in the Vector
    although then the getSelectedRow method works.

    Any guidance.
    Thank you
    Brett

  7. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable prepareRenderer changes all cell colors

    I tried using the rowAtPoint(evt.getPoint()) method to get the row number and it seems to have worked but I don't know if that's a good way to do it?

    Thanks for all the help
    Brett

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 313 Times in 293 Posts

    Default Re: JTable prepareRenderer changes all cell colors

    try adding setRowSelectionAllowed(true);
    I doubt it will do anything, but maybe it will. You never know.

  9. #9
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: JTable prepareRenderer changes all cell colors

    Quote Originally Posted by BrettStuart View Post
    Hi Copeg I have tried

    if (row == vRows.get(i)){
    comp.setBackground(Color.LIGHT_GRAY);
    }else{
    comp.setBackground(Color.WHITE);//or whatever your default color is
    }
    but this also only changes the color of the last row I change in the table eg. the last number in the Vector
    although then the getSelectedRow method works.
    This may be due to the way you are iterating through your Vector. Try something like
     
    if ( vRows.contains(row) ){
        comp.setBackground(Color.LIGHT_GRAY);
    }else{
       comp.setBackground(Color.WHITE);
    }
    Although after more thought, if you are just wanting to alter the selection color and not the basic rendering color, use the setSelectionBackground already discussed and don't override the prepareRenderer method.
    If you wish to receive all the selected rows, call getSelectedRows() which returns an array of all the selected rows, rather than the last on selected.

  10. #10
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable prepareRenderer changes all cell colors

    Hi guys

    if ( vRows.contains(row) ){ ... }, sorted the problem.

    Thank You so much for the help.
    Brett

Similar Threads

  1. How to start writing Java Apps for Cell Phones?
    By Jchang504 in forum Java ME (Mobile Edition)
    Replies: 7
    Last Post: January 10th, 2011, 04:11 AM
  2. How to create a database in my cell phone?
    By Viggopiano in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: July 12th, 2010, 07:26 AM
  3. [SOLVED] Filling in alternating colors of a "BullsEye"
    By forte in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 22nd, 2010, 06:05 PM
  4. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM
  5. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM