Change the Cell Color of a JTable on MouseOver

Change the Cell Color of a JTable on MouseOver

This code highlights the cell of a JTable when the cell is moused over. It includes its own Table cell renderer and a Mouse motion listener, for getting the mouse position on the JTable.

public class MouseHoverTable extends JFrame{    int itsRow =0;    int itsColumn = 0;    JTable itsTable;    MouseHoverTable(String framename)    {        super(framename);       itsTable = new JTable( 5, 3 );        //make the table transparent        itsTable.setOpaque(false);        itsTable.setDefaultRenderer(Object.class, newAttributiveCellRenderer());        MyMouseAdapter aMouseAda = new MyMouseAdapter();        itsTable.addMouseMotionListener(aMouseAda);        JScrollPane jsp = new JScrollPane(itsTable);        this.getContentPane().add(itsTable);//jsp);    }public static void main(String[] args){    JFrame frame = new MouseHoverTable("Table Example");    frame.addWindowListener( new WindowAdapter()    {    public void windowClosing(WindowEvent e)    {    Window w = e.getWindow();    w.setVisible(false);    w.dispose();    System.exit(0);    }    } );        frame.pack();;    }    public class    MyMouseAdapter extends MouseMotionAdapter //extendsMouseAdapter    {        public void mouseMoved(MouseEvent e)        {            JTable aTable =  (JTable)e.getSource();            itsRow = aTable.rowAtPoint(e.getPoint());            itsColumn = aTable.columnAtPoint(e.getPoint());            aTable.repaint();        }    }    public class AttributiveCellRenderer extends JLabel  implementsTableCellRenderer {  public AttributiveCellRenderer() {    setOpaque(true);  }  public Component getTableCellRendererComponent(JTable table, Objectvalue,                 boolean isSelected, boolean hasFocus, int row, intcolumn) {       if(row == itsRow && column == itsColumn)       {          this.setBackground(;          this.setForeground(;       }       else       {          this.setBackground(Color.cyan);          this.setForeground(Color.darkGray);       }        String aStr = "Row " + row+ "Column"+column;        this.setText(aStr);    return this;  }
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes