Results 1 to 6 of 6
  1. #1
    RickAintree is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Post How to change color of a cell in a Jtable based on a criteria

    Do you have to create a separate class?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by RickAintree View Post
    How to change color of a cell
    Ask the warden to have it painted?

  3. #3
    RickAintree is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    I wish it was that easy

  4. #4
    RickAintree is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    can you show me some code to give me an idea

  5. #5
    RickAintree is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default this is my table renderer but it is not working

    Java Code:
        public class MultiTargetColRenderer implements javax.swing.table.TableCellRenderer {
            java.awt.Color foreground;
            java.awt.Color background;
            java.awt.Font font;
    
            private javax.swing.table.DefaultTableCellRenderer MULTI_RENDERER = new javax.swing.table.DefaultTableCellRenderer();
    
            public java.awt.Component getTableCellRendererComponent(javax.swing.JTable tblTable, java.lang.Object objValue, boolean blnIsSelected, boolean blnHasFocus, int intRow, int intColumn) {
    
                java.awt.Component multirenderer = MULTI_RENDERER.getTableCellRendererComponent(tblTable, objValue, blnIsSelected, blnHasFocus, intRow, intColumn);
    
    
    
               if (tbeResults.getValueAt(0, 0).toString() == "9005250114") {
                    //Set font colour to white
                    foreground = java.awt.Color.blue;
    
    
                } else {
    
                    background   = new java.awt.Color(255,153,153);
                }
    
    
    
    
                //Assign colours
                multirenderer.setForeground(foreground);
                multirenderer.setBackground(background);
                multirenderer.setFont(font);
    
                return (multirenderer);
            }
        }
    I want it to look in the Status column to see whether it contains Active then change the color of text to blue else background pink. but it is only setting the background to pink and not all the cells are being changed either the first two are not changing. someone please help

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Set both background and foreground to the desired colors in both the if and else blocks.

    And why did you double post this?
    I Only need the color of a cell to change not the entire row

    db

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2011, 06:48 AM
  2. Can I change the border of a JPanel cell?
    By ryuzog in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 06:05 AM
  3. Jtable Sum of Sales based on criteria.
    By javamula in forum JDBC
    Replies: 3
    Last Post: 10-05-2009, 07:08 PM
  4. Color cell in JTable
    By ippacciani in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 11:53 AM
  5. Jframe In Jtable cell
    By Clarion in forum AWT / Swing
    Replies: 4
    Last Post: 06-23-2008, 04:42 AM

Posting Permissions

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