• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable editing on mouse click

 
G Estes
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic