Java Basic Problems

JTable cell Color: How to color your JTable individual cell background.

JTable Individual cell colorHere I am going to share with you a little solution by which you can color the background of individual JTable cell. In the following code I colored the cell’s background in click event i.e when you will click on the JTable Cell the background of that cell will be red. This solution is just to give you a basic idea; you can modify it as your own.  The code is given below.




import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

class JtableCellColor extends JFrame


private JPanel topPanel;

private JTable table;

private JScrollPane scrollPane;

private String[] columnNames=new String[3];

private String[][] dataValues=new String[3][3];

JTextField textBox=new JTextField();

public JtableCellColor()


setTitle(“JTable Cell Color”);


setDefaultCloseOperation (EXIT_ON_CLOSE);

topPanel=new JPanel();

topPanel.setLayout(new BorderLayout());


columnNames=new String[] {“Column 1″ ,”Column 2”, “Column 3”};

dataValues=new String[][]






TableModel  model=new myTableModel();

table=new JTable();



TableColumn soprtColumn=table.getColumnModel().getColumn(1);

soprtColumn.setCellEditor(new DefaultCellEditor (textBox));


scrollPane=new JScrollPane(table);


table.addMouseListener(new java.awt.event.MouseAdapter()  


public void mouseClicked(java.awt.event.MouseEvent e)





public class myTableModel extends DefaultTableModel






               public boolean isCellEditable(int row,int cols)


                          return true;



public static void main(String args[])


               JtableCellColor x=new JtableCellColor();




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s