Java Basic Problems

Java :How to know in which Row and Column I have clicked on JTable

In this post I have shared you the solution about how to know in which Row and Column of JTable I have clicked.

According to the solution, wherever you have clicked on the JTable the result will be shown using

Message Dialog Box (Check the OUTPUT picture below).


Code Example:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

class SimpleTableTest extends JFrame


private JPanel topPanel ;

private JTable table;

private JScrollPane scrollPane;

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

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

public SimpleTableTest()


setTitle(“JTable Cell Click”);


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( );



 scrollPane=new JScrollPane(table);


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


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


int row=table.rowAtPoint(e.getPoint());

int col= table.columnAtPoint(e.getPoint());

JOptionPane.showMessageDialog(null,”You have Clicked on Column :” +(col+1)+ ” : and Row : ” +(row+1));






            public class myTableModel extends DefaultTableModel


                                                    myTableModel( )



                                                  System.out.println(“Inside myTableModel”);


                                                public boolean isCellEditable(int row,int cols)


                                   return false;                                                                                         



            public static void main(String args[])


            SimpleTableTest mainFrame=new SimpleTableTest();




JTable Row and Column Click

1 thought on “Java :How to know in which Row and Column I have clicked on JTable”

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