Java Basic Problems

Java JTable Cell and JTextField: How to use and assign JTextField in JTable cell.

I think, to assign a JTextField in JTable cells is really important for the purpose of JTable cell validation. For many purpose in software development where we use JTable may need to use JTable cell validation  and for this validation purpose we may also need to assign and use JTextField in JTable cell. In the given Example, I have assigned and used  a JTextField  in the  Column 2  of the following JTable. Just Copy the code and Run it in your computer.


Code Example: 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

class JtableCellValidation 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 JtableCellValidation()


setTitle(“JTable Cell Validation”);


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


textBox.addKeyListener(new KeyAdapter()


public void keyTyped(KeyEvent e)


if(!Character.isDigit(e.getKeyChar()) && e.getKeyChar() !=KeyEvent.VK_BACK_SPACE)




JOptionPane.showMessageDialog(null,”String Type Entry Not Allowed”);








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[])


  JtableCellValidation x=new JtableCellValidation();




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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s