1
DefaultTableCellRenderer cellRender = new DefaultTableCellRenderer();
for(int i = 0; i < tblPackage.getRowCount(); i++)
{
    if("ACTIVE".equals(tblPackage.getModel().getValueAt(i, 3).toString()))
    {
                cellRender.setForeground(Color.GREEN);
    }
}

There's 4 columns in my table...and if the 4th columns of the data is equal to "ACTIVE" word, it will become to greeen color...how can I do that?? Is it got any problem with my logic??

jefferyleo
  • 630
  • 3
  • 17
  • 34

2 Answers2

4

You should override the getTableCellRendererComponent of the DefaultTableCellRenderer:

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column)
        {
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if(table.getColumnModel().getColumn(column).getIdentifier()
                    .equals(your_4th_column_identifier)) // or use getColumnAt(4)
            {
                if(value.toString().equals("ACTIVE"))
                {
                    c.setBackground(Color.GREEN);
                }
            }
            return c;
        }
wxyz
  • 709
  • 3
  • 8
  • means have to create a new class??? Since my existing java class is inheriting the JFrame class... – jefferyleo Jan 27 '14 at 07:08
  • no need to create a new class for your frame, just extend the DefaultTableCellRenderer and attach it to your table model – wxyz Jan 27 '14 at 07:15
  • how could I extend 2 classes?? public class AdminControlPanel extends javax.swing.JFrame ....As you can see...I already inherited JFrame LOL – jefferyleo Jan 27 '14 at 07:18
0

You can use the following:

    Jtable jtable = new JTable(dtm)
    {

        public Component prepareRenderer(TableCellRenderer renderer,int row,int column)
        {
              Component comp=super.prepareRenderer(renderer,row, column);
              if(column==3 && "ACTIVE".equals(tblPackage.getModel().getValueAt(row, 3).toString()))
              {
                     comp.setForeground(Color.GREEN);
              }
              return comp;

        }
     } ;
Rahul
  • 3,479
  • 3
  • 16
  • 28
  • tblPackage.getModel().getValueAt(i, 3).toString() NPE if value is null – StanislavL Jan 27 '14 at 07:12
  • but i have the value at there..no nullpointerexception happening – jefferyleo Jan 27 '14 at 07:14
  • @StanislavL If it can be null then you may keep a check if(column==3 && (tblPackage.getModel().getValueAt(row, 3)!=null) && "ACTIVE".equals(tblPackage.getModel().getValueAt(row, 3).toString())) – Rahul Jan 27 '14 at 07:16
  • So i using the for loop to check there's the data in the row or not – jefferyleo Jan 27 '14 at 07:19
  • @jefferyleo No need of for loop. It will use your data model, to render the cells of jtable and check the values in that cell. – Rahul Jan 27 '14 at 07:21
  • ohh...means the render will keep help you check whether there consisting data in every cell or not right?? – jefferyleo Jan 27 '14 at 07:23
  • I know lots of people seem to do this, but this really a bad solution. It's not the responsibility if the table to make these decisions, it's the responsibility to the renderers, that's there job. This makes the solution less portable or reusable - IMHO – MadProgrammer Jan 27 '14 at 07:41
  • Use an appropriate table cell renderer as described in [Using Custom Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer) – MadProgrammer Jan 27 '14 at 08:22