JTable Cell Click Event: Click on the JTable Cell and show the Value of that cell.

Sometimes while programming we may need to check and show the value of JTable Cell in click event.

Here, in the given example, I did the coding in such a way that while clicking on a cell of  JTable

It will show the value of that JTable cell in MessageDialog Box.

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

setSize(300,300);

topPanel= new JPanel();

topPanel.setLayout(new BorderLayout());

getContentPane().add(topPanel);

setDefaultCloseOperation(EXIT_ON_CLOSE);

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

dataValues = new String[][]     {

                                                {“1″,”2″,”3″},

                                                {“4″,”5″,”6″},

                                                {“7″,”8″,”9″}

                                            };

 TableModel model=new myTableModel();

 table =new JTable( );

 table.setRowHeight(50);

 table.setModel(model);

 scrollPane=new JScrollPane(table);

  topPanel.add(scrollPane,BorderLayout.CENTER);   

  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,” Value in the cell clicked :”+ ” ” +table.getValueAt(row,col).toString());

System.out.println(” Value in the cell clicked :”+ ” ” +table.getValueAt(row,col).toString());

}

}

);

}

            public class myTableModel extends DefaultTableModel

                                                                                    {

                                                    myTableModel( )

                                                 {

                                                   super(dataValues,columnNames);

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

                                                 }

                                    public boolean isCellEditable(int row,int cols)

                                     {

                                   return false;                                                                                         

                                    }

                                    }         

            public static void main(String args[])

            {

            SimpleTableTest mainFrame=new SimpleTableTest();

            mainFrame.setVisible(true);

            }         

                        }

OUTPUT:

show and check JTable cell value

About these ads

3 Responses to JTable Cell Click Event: Click on the JTable Cell and show the Value of that cell.

  1. Johnny says:

    And what if you click TAB?

  2. Wu Wenbo says:

    Thanks a lot for this help!

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 309 other followers