Dear all,

How do I set particular color in a specific cell in JTable using Netbeans ?

I have searched using google but not found any significant answer.

Thanks and regards,
Nirupam.

<td width="155" bgcolor="#999999">
       <span class="style3">
              <strong>EMPID</strong> 
      </span>
</td>

u put that above lines for required place for your convenient.

<td width="155" bgcolor="#999999">
       <span class="style3">
              <strong>EMPID</strong> 
      </span>
</td>

u put that above lines for required place for your convenient.

Can you please send me "the" implementation of this in Java? I would really love to see this...

PS: Proverb "measure twice, cut once" applies even here.

As the question is not so clear I posted this juste for insperation.
To colorize a cell you shod now the the underlying column of the model and the row to.
Of cors there is many way to do that the simple approche is as follow.

Here is an example of this :

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
 
public class TablePrepareRenderer extends JFrame
{
	JTable table;
 
	public TablePrepareRenderer()
	{
		Object[] columnNames = {"Type", "Company", "Shares", "Price"};
		Object[][] data =
		{
			{"Buy", "IBM", new Integer(1000), new Float(80.50)},
			{"Sell", "MicroSoft", new Integer(2000), new Float(6.25)},
			{"Sell", "Apple", new Integer(3000), new Float(7.35)},
			{"Buy", "Nortel", new Integer(4000), new Float(20.00)}
		};
 
		DefaultTableModel model = new DefaultTableModel(data, columnNames);
		table = new JTable( model )
		{
			//  Returning the Class of each column will allow different
			//  renderers to be used based on Class
			public Class getColumnClass(int column)
			{
				return getValueAt(0, column).getClass();
			}
 
			public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
			{
				Component c = super.prepareRenderer(renderer, row, column);
				if (!c.getBackground().equals(getSelectionBackground()))
				{
					String type = (String)getModel().getValueAt(row, 0);
					c.setBackground( type.equals("Buy") ? Color.GREEN : Color.YELLOW );
				}
				return c;
			}
		};
		table.setPreferredScrollableViewportSize(table.getPreferredSize());
		JScrollPane scrollPane = new JScrollPane( table );
		getContentPane().add( scrollPane );
	}
 
	public static void main(String[] args)
	{
		TablePrepareRenderer frame = new TablePrepareRenderer();
		frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
		frame.pack();
		frame.setLocationRelativeTo( null );
		frame.setVisible(true);
	}
}

hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.