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.setCellRenderer(renderer);

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

	}

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

		try{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception e){
			e.printStackTrace();
		}

		final String[] FAKE_NAMES_ARRAY = {
				"aaa",
				"aab",
				"aac",
				"baa",
				"bab",
				"bac",
				"caa",
				"cab",
				"cac",
				"cad",
		};

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

		String[] columnNameArray = {
				TEXT_COLUMN_HEADER,
				"1",
				"2",
		};

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

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

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

		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLocation(300, 200);
		frame.pack();
		frame.setVisible(true);

	}

}

/**
 * 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;
	}

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

	@Override
	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) {
			jTextField.setText(value.toString());
			if (value.toString().length() <= 1) {
				// do something
			}
			else { // length is 2 or more
				// do something else
			}
		}

		return jTextField;
	}

}

Open in new window

LVL 1
allelopathAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJConnect With a Mentor Commented:
Use DocumentListener on the Document of the text field
0
 
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.
0
 
CEHJCommented:
Yes, any text component has a Document
0
 
CEHJCommented:
:-)
0
All Courses

From novice to tech pro — start learning today.