capture keystrokes in JTextfield in JTable

I am have a JTable with a JTextField cell. I am trying to capture keystrokes in the cell. I think I don't want a KeyListener because, even though I want to capture each keystroke, at each keystroke I need to know the entire contents of the JTextField. In the code below the value in getTableCellEditorComponent() is always "". How do I do this?
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class MyTable_TextField extends JTable {

	private DefaultTableModel defaultTableModel;

	private Object[][] dataArray;
	private Object[] columnNameArray;

	private final int TEXT_COLUMN = 0;	

	// column headers
	private static final String TEXT_COLUMN_HEADER = "Text";

	// text field (one line)
	private MyTextFieldEditor textFieldEditor;

	private List<String> nameList;

	 * constructor
	 * @param dataNameArray
	 * @param columnNameArray
	public MyTable_TextField(Object[][] dataNameArray, Object[] columnNameArray, List<String> nameList) {

		this.dataArray = dataNameArray;
		this.columnNameArray = columnNameArray;

		defaultTableModel = new DefaultTableModel(dataNameArray, columnNameArray);
		this.setModel(defaultTableModel)	;

		this.nameList = nameList;

		// text field
		TableColumn modelColumn = this.getColumnModel().getColumn(TEXT_COLUMN);

		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); // renderer
		//renderer.setToolTipText("Double click to type"); // tooltip

		modelColumn.setCellEditor(new MyTextFieldEditor(nameList)); // editor


	 * @param args
	public static void main(String[] args) {

		catch (Exception e){

		final String[] FAKE_NAMES_ARRAY = {

		List<String> nameList = new ArrayList<String>();
		// fake data for now
		for (String name : FAKE_NAMES_ARRAY) {

		String[] columnNameArray = {

		Object[][]  dataArray = { 	{"", "", ""}, 	};

		final MyTable_TextField panel = new MyTable_TextField(dataArray, columnNameArray, nameList);

		final JFrame frame = new JFrame();
		frame.getContentPane().add(new JScrollPane(panel));
		frame.setPreferredSize(new Dimension(500, 200));
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {

		frame.setLocation(300, 200);



 * nested class
class MyTextFieldEditor extends DefaultCellEditor {

	private List<String> nameList;
	private String value;

	 * constructor
	public MyTextFieldEditor(List<String> nameList) {
		super(new JTextField());
		this.nameList = nameList;

	public Object getCellEditorValue() {
		System.out.println("getCellEditorValue(): " + value);
		return value;

	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
			int row, int column) {
		System.out.println("getTableCellEditorComponent(): " + value);

		this.value = (String) value;
		JTextField jTextField = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);

		if (value != null) {
			if (value.toString().length() <= 1) {
				// do something
			else { // length is 2 or more
				// do something else

		return jTextField;


Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use DocumentListener on the Document of the text field

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
allelopathAuthor Commented:
I had avoided using DocumentListener, thinking that a JTextField being just one line was not really enough to be a document, but it seems to work.
Yes, any text component has a Document
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.