6 Replies Latest reply: Sep 12, 2012 12:10 AM by DarrylBurke RSS

    change the color of a particular row in a jtable

    888525
      I have a jtable and i want to change the color of one particular column and few rows to blue and underline the text depending on some condition. In my view class if a condition is true

      for( count=0;count<grdTest.getRowcount;count++)
      {
      if(some condition true)
      {
      grdTest.getColumnModel().getColumn(0).
      setCellRenderer(new MyTestCellRenderer(count));
      }

      }

      And my cell renderer


      public class MyTestCellRenderer extends DefaultTableCellRenderer {

      int rowIndex=-1;
      public MyTestCellRenderer(int rowcount) {
      super();
      rowIndex=rowcount;
      }

      public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
      super.getTableCellRendererComponent(table, value,
      isSelected, hasFocus, row, column);
      if(row == rowIndex)
      {
      setText("<html> <u> <font color='blue'>" + value.toString());

      }

      return this;
      }

      }


      This is behaving very wierd . If suppose there are 50 rows and all of them have the condition true then only the last one in the row color is changes.
      However if only one out othe say10 has the condition true then it changes the color and underlines the particular row,column.

      Is there any other way to do this or what i am doing wrong

      Thanks
        • 1. Re: change the color of a particular row in a jtable
          DarrylBurke
          Announcements at the top of the forum listing are meant to be read. Clearly, you didn't bother.

          133 posts and member for nearly a full year and you still haven't learned how to post code?

          db
          • 2. Re: change the color of a particular row in a jtable
            DrClap
            if(row == rowIndex)
            Your code is a shambles because of the lack of
             tags, but I don't believe I see anything being called when this boolean condition is not true. That would be a problem.                                                                                                                                                                                                                                                                                                                                                                                                            
            • 3. Re: change the color of a particular row in a jtable
              888525
              Sorry for not posting the code in correct format.Here is the code in proper format.
              for( count=0;count<grdTest.getRowcount;count++)
              {
              if(some condition true)
              {
              grdTest.getColumnModel().getColumn(0).
              setCellRenderer(new MyTestCellRenderer(count));
              }
              
              }
              And my cell renderer
              public class MyTestCellRenderer extends DefaultTableCellRenderer {
              
              int rowIndex=-1;
              public MyTestCellRenderer(int rowcount) {
              super();
              rowIndex=rowcount;
              }
              
              public Component getTableCellRendererComponent(JTable table, Object value,
              boolean isSelected, boolean hasFocus, int row, int column) {
              super.getTableCellRendererComponent(table, value,
              isSelected, hasFocus, row, column);
              if(row == rowIndex)
              {
              setText("<html> " + value.toString());
              
              
              }
              
              
              return this;
              }
              
              }
              • 4. Re: change the color of a particular row in a jtable
                888525
                Thanks for the reply.What is happening is if I give any system out in the for loop

                like
                System.out.Println("The count is" + count)
                Then first all the count gets printed and then any print statement inside the cell renderer.
                If for all rows the condition is true then it takes the last one .

                There could be random any number of rows which should have the color change depending on the If condition ,so how do I manage this i.e what
                condition I put in my cell renderer based on which it should change the color.


                Please help.

                Thanks
                • 5. Re: change the color of a particular row in a jtable
                  DrClap
                  Like I said before (although not all that clearly): Write your cell renderer so that it always specifies the colour of the cell it's rendering. Your code only sometimes specifies the colour, and other times lets it remain whatever colour it was before.

                  And don't assume the cell renderer is only called when the table is initially displayed.
                  • 6. Re: change the color of a particular row in a jtable
                    DarrylBurke
                    885522 wrote:
                    Sorry for not posting the code in correct format.Here is the code in proper format.
                    http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

                    Learn to indent code for readability.

                    db