startCellEditing() in DefaultCellEditor?

I have a Jtable which contains a JTextField in the first column. When one of these cells is clicked, a popup menu is displayed. When a menu selection is made, the value chosen is returned to the parent component, the JTextField. When another cell is clicked, the JTextField still contains the data from the previous click, so I want to load the contents of the new cell into JTextField. I have this class:

class MyTextFieldEditor extends DefaultCellEditor

and I need a "startCellEditing()" method in DefaultCellEditor, but I don't see anything like that.
What can I do?

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import tableFocus.InvisibleTableFocusComponent;
import constraintEditor.ConstraintConstants.MainToolBarActions;

public class MyTable_TextField extends JTable implements PropertyChangeListener{

	private JComponent parentComponent;
	
	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 editor (one line)
	private JTextField  jTextField;

	// text field editor
	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;

		TableColumn modelColumn = this.getColumnModel().getColumn(TEXT_COLUMN);

		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); // renderer
		//renderer.setToolTipText("Double click to type"); // tooltip
		modelColumn.setCellRenderer(renderer);
		
		// text field
		jTextField = new JTextField();
		this.parentComponent = jTextField;
		jTextField.addPropertyChangeListener(this);
		
		textFieldEditor = new MyTextFieldEditor(jTextField, nameList);
		modelColumn.setCellEditor(textFieldEditor); // editor

		MyDocumentListener documentListener = new MyDocumentListener(nameList);
		jTextField.getDocument().addDocumentListener(documentListener);

	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		String propertyName =  arg0.getPropertyName(); // get which property changed
		System.out.println ("MyTable_TextFieldl.propertyChange() from " + arg0.getOldValue() + " to " + arg0.getNewValue());
		System.out.println ("\tproperty name: " + arg0.getPropertyName());

		// when tableFocusComponent is changes, we set the name so as to flag that data has changed
		if (propertyName.equalsIgnoreCase("value")) {


		}
	}

	
	/**
	 * nested class
	 *
	 */
	class MyDocumentListener implements DocumentListener {

		private List<String> nameList;
		
		/**
		 * constructor
		 * 
		 * @param nameList
		 */
		public MyDocumentListener (List<String> nameList) {
			this.nameList = nameList;
		}
		
		@Override
		public void insertUpdate(DocumentEvent e) {
			//System.out.println ("insert update");
			doSomething(e);
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			//System.out.println ("remove update");
			doSomething(e);
		}
		@Override
		public void changedUpdate(DocumentEvent e) {
			//Plain text components do not fire these events
		}

		/**
		 * 
		 * @param documentEvent
		 */
		public void doSomething(DocumentEvent documentEvent) {
			
			Document document = (Document) documentEvent.getDocument();
			
			int stringLength = document.getLength();
			if (stringLength >= 2) {
				String refineString= "";
				try {
					refineString = document.getText(0, document.getLength());
				} catch (BadLocationException e2) {
					System.out.println ("MyDocumentListener.doSomething() ");
					e2.printStackTrace();
				}

				// popup variable list
				NamePopupMenu namePopupMenu = NamePopupMenu.getInstance();
				namePopupMenu.initialize(parentComponent, nameList, refineString);
			}
			
		}
	}

	/**
	 * 
	 * @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","aaab", "aaac", "aaad","aaaab", "aaaac", "aaaad","aaaaab", "aaaaac", "aaaaad",
				"bba", "bbb",	"bbc",
				"cca", "ccb", 	"ccc", "ccd",
				"dda", "ddb",	"ddc",
				"eea", "eeb",	"eec",
				"ffa", "ffb",	"ffc",
				"gga", "ggb",	"ggc",
				"hha", "hhb",	"hhc",
				"gga", "ggb",	"ggc",
				"hha", "hhb",	"hhc",
				"iia", "iib",	"iic",
				"jja", "jjb",	"jjc",
				"kka", "kkb",	"kkc",
				"lla", "llb",	"llc",
				"mma", "mmb",	"mmc",
				"nna", "nnb",	"nnc",
				"ooa", "oob",	"ooc",
				"ppa", "ppb",	"ppc",
				"qqa", "qqb",	"qqc",
				"rra", "rrb",	"rrc",
				"ssa", "ssb",	"ssc","sssa", "sssb",	"sssc","ssssa", "ssssb","ssssc","sssssa", "sssssb",	"sssssc",
				"tta", "ttb",	"ttc",
				"uua", "uub",	"uuc",
		};

		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 = { 	{"", "", ""},
				{"", "", ""}
		};

		JFrame frame = new JFrame();
		JPanel jPanel = new JPanel();

		MyTable_TextField myTable_TextField = new MyTable_TextField(dataArray, columnNameArray, nameList);
		jPanel.add(myTable_TextField);
		
		frame.getContentPane().add(new JScrollPane(myTable_TextField));
		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);

	}

}

/**
 *
 */
class MyTextFieldEditor extends DefaultCellEditor {

	private JTextField  jTextField;
	private List<String> nameList;
	private String value;

	/**
	 * constructor
	 */
	public MyTextFieldEditor(JTextField jTextField, List<String> nameList) {
		//super(new JTextField());
		super(jTextField);
		System.out.println("MyTextFieldEditor.constructor(): " + jTextField.getText());
		this.jTextField = jTextField;
		this.nameList = nameList;
	}

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

	@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) super.getTableCellEditorComponent(table, value, isSelected, row, column);

		return jTextField;
	}

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

}

-------------------------

package editorPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class NamePopupMenu extends JPanel implements EditorConstants {
	
	private static NamePopupMenu instance = null; // singleton
	
	private JPopupMenu popup;
	private JPopupMenu invisiblePopup;

	private JTextField parentComponent;
	
	private List<String> nameList;
	
	private int xCoordinate;
	private int yCoordinate;

	private String SELECT_NAME = "Select a name below";

	/**
	 * constructor
	 * do nothing because of singleton, call initialize() instead
	 */
	public NamePopupMenu() {

	} // end constructor
	
	/**
	 * singleton
	 * 
	 * @return
	 */
	public static NamePopupMenu getInstance()
	{
		if (instance == null) {
			instance = new NamePopupMenu();
		}
		return instance;
	}
	


	/**
	 * constructor
	 * 
	 * @param name list
	 * @param String to refine name list by
	 */
	public void initialize(JComponent parentComponent, List<String> bigNameList, String refineString) {
		
		this.parentComponent = (JTextField) parentComponent;
		
		// if popup doesn't exist yet, create it
		if (popup == null) {
			popup = new JPopupMenu();
		}
		else { // popup does exist, so remove any previous menu items
			popup.setVisible(false);
			popup.removeAll();
			// need to resize
		}

		this.nameList = new ArrayList<String>();
		Iterator<String> bigNameIterator = bigNameList.iterator();
		while (bigNameIterator.hasNext()) {
			String name = new String(bigNameIterator.next());
			// if this name starts with same characters as refineString then remove it from the list
			if ( name.startsWith(refineString)) {
				this.nameList.add(name);
			}
		}		

		// parent must be showing to get screen location
		if (parentComponent.isShowing()) {
			xCoordinate = parentComponent.getLocationOnScreen().x;
			yCoordinate =  parentComponent.getLocationOnScreen().y;
		}
		else {
			xCoordinate = 0;
			yCoordinate =  0;
		}
		this.setLocation(xCoordinate, yCoordinate);
		
		setComponent();		
	}
	
	/**
	 * 
	 */
	private void setComponent() {
		
		invisiblePopup  = new JPopupMenu();	
		
		ActionListener menuListener = new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				//System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed.");
				menuItemSelected(event.getActionCommand());

			}
		};
			
		// set items in menulist
		setMenuItem(menuListener, SELECT_NAME);

		setMenuItemInvisible(menuListener, SELECT_NAME);

		Iterator<String> nameIterator = nameList.iterator();
		while (nameIterator.hasNext()) {
			String name = nameIterator.next();
			setMenuItem(menuListener, name);
			setMenuItemInvisible(menuListener, name);
		}

		popup.setBorder(new BevelBorder(BevelBorder.RAISED));
		invisiblePopup.setBorder(new BevelBorder(BevelBorder.RAISED));
		
		popup.addPopupMenuListener(new PopupPrintListener());

		//System.out.println ("invisiblePopup size: " + invisiblePopup.getPreferredSize());
		popup.setPreferredSize(invisiblePopup.getPreferredSize());
		//popup.setPreferredSize(new Dimension(200,300));
		
		popup.setVisible(true);
		popup.setLocation(xCoordinate, yCoordinate);

		addMouseListener(new MousePopupListener());	
	}


	private void setMenuItem(ActionListener menuListener, String name) {
		JMenuItem item = new JMenuItem(name);
		popup.add(item);
		item.setHorizontalTextPosition(JMenuItem.RIGHT);
		item.addActionListener(menuListener);
	}

	private void setMenuItemInvisible(ActionListener menuListener, String name) {
		JMenuItem item = new JMenuItem(name);
		invisiblePopup.add(item);
		item.setHorizontalTextPosition(JMenuItem.RIGHT);
		item.addActionListener(menuListener);
	}

	
	/**
	 * called when menu item is selected
	 */
	public void menuItemSelected(String actionCommand) {

		//System.out.println("menuItemSelected(): " + actionCommand);
		
		if (actionCommand.equalsIgnoreCase(SELECT_NAME)) {
			System.out.println("\tdo nothing");
		}
		else { // its a name, so return it
			//System.out.println("\treturn to parent with name");
			
			// set value of parent to the menu item selection
			parentComponent.setText(actionCommand);
			
			popup.setVisible(false); // hide popup menu
			// set value of operator button to the menu item selection

		}

	}

	/**
	 * nested class to check whether mouse events are the popup trigger
	 */
	class MousePopupListener extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			checkPopup(e);
		}

		public void mouseClicked(MouseEvent e) {
			checkPopup(e);
		}

		public void mouseReleased(MouseEvent e) {
			checkPopup(e);
		}

		private void checkPopup(MouseEvent e) {
			if (e.isPopupTrigger()) {
				popup.show(NamePopupMenu.this, e.getX(), e.getY());
			}
		}
	}

	/**
	 * nested class to show when popup events occur
	 */
	class PopupPrintListener implements PopupMenuListener {

		public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
			System.out.println("Popup menu set to visible");
		}

		public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
			System.out.println("Popup set to invisible");
		}

		public void popupMenuCanceled(PopupMenuEvent e) {
			System.out.println("Popup menu is hidden");
		}
	}

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

		String refineString = "";
		
		List<String> nameList = new ArrayList<String>();
		nameList.add("aaa");
		nameList.add("aab");
		nameList.add("aac");
		nameList.add("baa");
		nameList.add("bab");
		nameList.add("bac");
		nameList.add("caa");
		nameList.add("cab");
		nameList.add("cac");
		nameList.add("cad");
		
		JFrame frame = new JFrame("Popup Menu Example Visible");
		JTextField jTextField = new JTextField();
		JPanel jPanel = new JPanel();
		jPanel.add(jTextField);
		
		// popup name list
		NamePopupMenu nameePopupMenu = NamePopupMenu.getInstance();
		nameePopupMenu.initialize(jTextField, nameList, refineString);
		jPanel.add(nameePopupMenu);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(nameePopupMenu);
		frame.setSize(300, 300);
		frame.setLocation(300,300);
		frame.setVisible(true);
	}
}

Open in new window

LVL 1
allelopathAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

CEHJCommented:
Why are you not using the JTextField as an editor firectly?
0
allelopathAuthor Commented:
The JTextField is in the editor:
class MyTextFieldEditor extends DefaultCellEditor {

      private JTextField  jTextField;

If this is not the way, can you show me how?
0
Mick BarryJava DeveloperCommented:
> If this is not the way, can you show me how?

yes that is the way. you would *not* use a JTextField directly
0
Mick BarryJava DeveloperCommented:
load them in getTableCellEditorComponent()
0

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:
ok, getTableCellEditorComponen() basically is startCellEditing()
0
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.