GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JTable editing on mouse click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable editing on mouse click" Watch "JTable editing on mouse click" New topic
Author

JTable editing on mouse click

G Estes
Ranch Hand

Joined: Oct 22, 2002
Posts: 62
I have a custom renderer/editor that uses a JTextField. I click w/ the mouse on a cell...the cell is "selected" but no caret is visible. I enter text and it appears at the end of the String, but still no caret.
What I want is to click on a cell and the cell become editable w/ the caret positioned where the mouse click occured.
Suggestions?
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
By default, the cursor will appear when the cell is double clicked.
I don't know, if it would help to set the DefaultCellEditor.setClickCountToStart() to 1. Of course, that will work only in the case you are using the DefaultCellEditor or a subclass of it.
Another possibility might be, to set the caret of the Editor (as a subclass of JTextField/JTextComponent) explicitely). there most likely is some method in the API that does this. If not in the JTextComponent hierarchy than in Document or its subclasses (something like setCaret(int position)).
Chantal
Keith L
Greenhorn

Joined: May 23, 2004
Posts: 24
This thread is a couple of years old but I've run into exactly the same problem. I wondered if anyone has any further suggestions.
Keith L
Greenhorn

Joined: May 23, 2004
Posts: 24
Okay, the code below does what I want. I'd have thought there would be some way form me to call setClickCountToStart() on the system-supplied DefaultCellEditor rather than creating a new one, but I couldn't find it.


TableColumn col = envtable.getColumnModel().getColumn(1);
DefaultCellEditor singleclick = new DefaultCellEditor(new JTextField());
singleclick.setClickCountToStart(1);
col.setCellEditor (singleclick);
 
GeeCON Prague 2014
 
subject: JTable editing on mouse click