Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By KevinWorkman
  • 1 Post By KevinWorkman
  • 1 Post By KevinWorkman

Thread: JTable Row Color

  1. #1
    ellias2007 is offline Member
    Join Date
    Feb 2010
    Posts
    25
    Rep Power
    0

    Unhappy JTable Row Color

    Hi all,
    in order to set the background color to each row that has some value (1000 for example) in some colon, i made this DefaultTableCellRenderer :

    class RowColorRenderer implements TableCellRenderer {

    public static final DefaultTableCellRenderer MyDTCR = new DefaultTableCellRenderer();

    public Component getTableCellRendererComponent(JTable TheTable,Object TheValue,boolean isSelected,boolean hasFocus,int row,int column)
    { Component renderer = MyDTCR.getTableCellRendererComponent(TheTable, TheValue, isSelected, hasFocus, row, column);
    ((JLabel) renderer).setOpaque(true);
    Color foreground, background;
    if (isSelected) {
    foreground = Color.yellow;
    background = Color.green;
    }
    else { if (TheTable.getValueAt(row,7).toString().equals("100 0")) {foreground = Color.blue;
    background = Color.red;
    }
    else {foreground = Color.white;
    background = Color.blue;
    }
    }

    renderer.setForeground(foreground);
    renderer.setBackground(background);
    return renderer;
    }
    }

    But, it does NOT work...
    Thanks to anyone tell me what is wrong in the code ....
    Last edited by ellias2007; 01-23-2012 at 07:26 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: JTable Row Color

    Sorry, but you telling us that it does not work is as useful to us as us telling you to fix it is to you. If you want help, you'll have to provide an SSCCE that demonstrates what you're talking about, and you'll have to be more specific about what's not working about it. Does it throw an Exception? Does it not display at all? Does it attain sentience and declare war on humanity?

    Hint: Don't use == on Strings. Do a search on these forums or google for why not.
    ellias2007 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ellias2007 is offline Member
    Join Date
    Feb 2010
    Posts
    25
    Rep Power
    0

    Default Re: JTable Row Color

    Sorry KevinWorkman... here some details

    Simply when i run the code, the background in any row that contains the value 1000 in the colon number 7 in NOT red ...
    All the rows have a BLUE background...
    i think that the problem is in :
    if (TheTable.getValueAt(row,7).toString().equals("100 0"))
    many thanks to any help
    Sorry again...
    Last edited by ellias2007; 01-23-2012 at 07:27 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: JTable Row Color

    Like I said, don't use == when comparing String. Use the equals() method instead. The == operator, when dealing with Objects, simply compares whether two instances are the same, not whether the values they hold are the same.
    ellias2007 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: JTable Row Color

    Did you not like this answer for some reason?

    Crossposted: JTable Row Color
    ellias2007 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

Similar Threads

  1. JTable Cell Color Follow Drag Event
    By joeyvitoro in forum AWT / Swing
    Replies: 6
    Last Post: 11-30-2011, 06:36 PM
  2. Replies: 6
    Last Post: 05-14-2011, 06:48 AM
  3. Replies: 5
    Last Post: 12-20-2010, 08:31 PM
  4. Color cell in JTable
    By ippacciani in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 11:53 AM
  5. Color of the focued row in a JTable
    By SteM in forum AWT / Swing
    Replies: 2
    Last Post: 11-20-2007, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •