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 3 of 3

Thread: JTable Row Color

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,548
    Thanks
    24
    Thanked 299 Times in 282 Posts

    Default JTable Row Color

    Ok, this is sort of odd so follow me here.

    I have a JTable of X number of rows (changes throughout program) and 11 columns. Each row contains details from an object I made. The object associated with each row has a boolean value in its class. What I am wanting to do is set the background color blue for every row whose object's boolean value is TRUE.

    It is my preference that I would not need to add a column for the boolean JUST so the rows can be colored. But if there is no other way, then so be it. It should also be noted that two of my columns are unique, in that they are JButtons instead of the standard JLabel (or whatever it is). The background colors of these JButtons should not change alongside with the rest of the row.

    I've been trying to figure this out for like 2 hours now. All my google searches regarding JTable background color has resulted in either Coloring Columns or Coloring based on user selection. Both of which are completely irrelevant for what I am attempting.

    Any help is appreciated, I'm getting real frustrated. Thank god for Slacker Radio and MJ keeping me from going insane.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: JTable Row Color

    You could either override the prepareRenderer method of JTable, or create a custom TableCellRenderer. Below is semi-pseudo code for the first method
     
    //this set contains the indexes of the columns which have JButtons
    Set<Integer> buttonColumns = new HashSet<Integer>();
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col){
        JComponent c = (JComponent)super.prepareRenderer(renderer,row,col);
     
        if ( !buttonColumns.contains(col){
            if ( ..check for condition to color red..){
                c.setBackground(Color.RED);
            }else{//color to default
                c.setBackground(getBackground());
            }
            c.setOpaque(true);
        }else{
            c.setBackground(c.getBackground());
            c.setOpaque(false);
       }
        return c;
    }

    The above will have to be messed with a bit to get it to work with what you have, but will hopefully get you started.

  3. The Following User Says Thank You to copeg For This Useful Post:

    aussiemcgr (October 8th, 2010)

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,548
    Thanks
    24
    Thanked 299 Times in 282 Posts

    Default Re: JTable Row Color

    Quote Originally Posted by copeg View Post
    You could either override the prepareRenderer method of JTable, or create a custom TableCellRenderer. Below is semi-pseudo code for the first method
     
    //this set contains the indexes of the columns which have JButtons
    Set<Integer> buttonColumns = new HashSet<Integer>();
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col){
        JComponent c = (JComponent)super.prepareRenderer(renderer,row,col);
     
        if ( !buttonColumns.contains(col){
            if ( ..check for condition to color red..){
                c.setBackground(Color.RED);
            }else{//color to default
                c.setBackground(getBackground());
            }
            c.setOpaque(true);
        }else{
            c.setBackground(c.getBackground());
            c.setOpaque(false);
       }
        return c;
    }

    The above will have to be messed with a bit to get it to work with what you have, but will hopefully get you started.
    You sir, are a life saver.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Color Problem
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 03:53 PM
  2. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM
  3. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  4. Checkbox - Change Font Color
    By wakebrdr77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 09:57 AM
  5. How prograssbarColor with differentThe color indicates
    By NARs in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2009, 10:04 PM