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

Thread: Change Jtable Row and cell Color

  1. #1

    Default Change Jtable Row and cell Color

    hi
    i have a jtable and I want to change the color of any row whose column value is equal to the value I want
    i use this code
     
    {
     
        public Component prepareRenderer ( TableCellRenderer renderer, int row, int column ){
            String status = (String)jTableAzan.getModel().getValueAt(row, 1);
            System.out.println(status);
            if (jtxtshamsi.getText().equals(status)) {
                setBackground(Color.GREEN);
            }
            Component component = super.prepareRenderer(renderer,row,column);
            //System.out.println("getModel= " + getModel().getValueAt(row,column));
     
            if (getModel().getValueAt(row,column) == null){
                component.setBackground(Color.ORANGE);
            }
            else if (getModel().getValueAt(row,column) != null){
                Object value = getModel().getValueAt(row,column).toString();
                if (isRowSelected(row))
                {
                    component.setBackground(new Color(56,105,138));
                    component.setForeground(Color.WHITE);
     
                    if(value.equals("Friday")){
                        component.setForeground(Color.red);
                    }
                }
     
                else if(value.equals("Friday")){
                    component.setBackground(Color.ORANGE);
                    component.setForeground(Color.red);
     
                }
     
                else{
     
                    if (row%2 == 0){
                        component.setBackground(new Color(255, 255, 253));
                        component.setForeground(Color.BLACK);
     
                    }
                    else {
                        component.setBackground(new Color(208,205,205));
                        component.setForeground(Color.BLACK);
                    }
                }
     
            }
            return component;
        }
     
    }


    But this part of my code does not work

     
     String status = (String)jTableAzan.getModel().getValueAt(row, 1);
            System.out.println(status);
            if (jtxtshamsi.getText().equals(status)) {
                setBackground(Color.GREEN);
            }
    Last edited by cnmeysam; March 24th, 2021 at 03:32 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Change Jtable Row Color

    part of my code does not work
    I don't see that code in the renderer code.

    Here is a similar problem: https://www.javaprogrammingforums.co...alue-cell.html
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 24th, 2021)

  4. #3

    Default Re: Change Jtable Row Color

    in my code if cell value equals Friday cell background and forgrand color is change and Lines whose row is odd change color
    i just need in column 2 if cell value is "1400/01/04" that row background color change to green in this code how i can do that?
    Last edited by cnmeysam; March 24th, 2021 at 09:03 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Change Jtable Row Color

    Can you make a small, simple, complete program that compiles and executes to show the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 24th, 2021)

  7. #5

    Default Re: Change Jtable Row Color

    thanks but i found solution this is my code now and works
    i write my code in here
    https://programsfuture.com/wp-content/uploads/2021/03/1.jpg
    https://programsfuture.com/wp-content/uploads/2021/03/2.jpg
    ( before ; )
     
    {
     
        public Component prepareRenderer ( TableCellRenderer renderer, int row, int column ){
     
            String status = (String)jTableAzan.getModel().getValueAt(row, 1);
     
            Component component = super.prepareRenderer(renderer,row,column);
            //System.out.println("getModel= " + getModel().getValueAt(row,column));
            if (jtxtshamsi.getText().equals(status)){
                component.setBackground(Color.GREEN);
            }
            else if (getModel().getValueAt(row,column) == null){
                component.setBackground(Color.ORANGE);
            }
            else if (getModel().getValueAt(row,column) != null){
                Object value = getModel().getValueAt(row,column).toString();
                if (isRowSelected(row))
                {
                    component.setBackground(new Color(56,105,138));
                    component.setForeground(Color.WHITE);
     
                    if(value.equals("Friday")){
                        component.setForeground(Color.red);
                    }
                }
     
                else if(value.equals("Friday")){
                    component.setBackground(Color.ORANGE);
                    component.setForeground(Color.red);
     
                }
     
                else{
     
                    if (row%2 == 0){
                        component.setBackground(new Color(255, 255, 253));
                        component.setForeground(Color.BLACK);
     
                    }
                    else {
                        component.setBackground(new Color(208,205,205));
                        component.setForeground(Color.BLACK);
                    }
                }
     
            }
            return component;
     
        }
     
    }
    Last edited by cnmeysam; March 24th, 2021 at 09:26 AM.

Similar Threads

  1. change color according to the value of a cell
    By paps in forum Java Theory & Questions
    Replies: 12
    Last Post: July 30th, 2020, 12:36 PM
  2. Replies: 2
    Last Post: June 9th, 2014, 04:06 PM
  3. How do I change the color of my cursor?
    By Chokladmunken in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2014, 10:54 AM
  4. JTable Row Color
    By ellias2007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 23rd, 2012, 01:29 PM
  5. JTable Row Color
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2010, 03:59 PM

Tags for this Thread