Hi Michael,
Thanks for the reply.
I managed to get it working with this code:
private void doEdit(){
TableViewModel model =
(TableViewModel)((TableSorter)fTable.getTable().getModel()).getTableModel();
model.setColumnEditable(new boolean [] {true,false});
int row =
fTable.getTable().getSelectionModel().getAnchorSelectionIndex();
// Enable the ability to select a single cell
fTable.getTable().setColumnSelectionAllowed(true);
fTable.getTable().setRowSelectionAllowed(true);
boolean success = fTable.getTable().editCellAt(row, 0);
if (success) {
// Select cell
boolean toggle = false;
boolean extend = false;
fTable.getTable().changeSelection(row, 0, toggle, extend);
DefaultCellEditor editor =
(DefaultCellEditor)fTable.getTable().getCellEditor();
JTextField textfield = (JTextField)editor.getComponent();
textfield.setCaretPosition(textfield.getText().length());
textfield.getCaret().setVisible(true);
}
}
Regards,
Enrico
-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Michael Wiles
Sent: 13 March 2006 05:38 PM
To: CTJUG Forum
Subject: [CTJUG Forum] Re: Editing a cell in a JTable
You have registered a cell editor against that cell I assume - this
is mandatory. There are a number of ways you can do this, but the
easiest is to register the call against the class type of the column.
It does mean that every row in that column must have the same type.
You are going to have to customize this particular editor though.
The route to take is to run some experiments...
Sub class DefaultCellEditor and put logging on each method call, and
then call super. If you look at the source of
DefaultCellEditor.EditorDelegate it is as follows...
/**
* Returns true if <code>anEvent</code> is <b>not</b> a
* <code>MouseEvent</code>. Otherwise, it returns true
* if the necessary number of clicks have occurred, and
* returns false otherwise.
*
* @param anEvent the event
* @return true if cell is ready for editing, false otherwise
* @see #setClickCountToStart
* @see #shouldSelectCell
*/
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
return ((MouseEvent)anEvent).getClickCount() >=
clickCountToStart;
}
return true;
}
This is the default cell editor and it turns on, as you can see, when
the event is a mouse event and the click count is >= clickCountToStart.
This is the method you should log on. As far as I can tell it's the
method which tells the cell when it should become editable. The
difficulty with setting cell editable is that the cell needs the focus,
and generally, that is controlled by the mouse. In your case you'll
need to fire an event at the cell you want to edit and inside that
event will contain information to indicate that it was invoked by a
popup menu or something.
You definitely have to override this method in your customized editor
so that the default editing behaviour does not operate.
Looks like the method JTable.editCellAt might also help, all you need
to do is convert the x,y co-ordinate of the right click into table row
column co-ordinates before you call this method. You could then create
a custom Event which when passed into your sub classed default cell
editor returns true on the isEditable call (very important).
Whenever you do swing coding a lot of experimentation is required
because it's complicated and you need serious documentation, not just
tutorials.. Rest assured, it is possible. It might be that swing is in
fact using this editCellAt method to actually edit the cells.
Sub class the JTable class and put logging on any of the methods that
you want to know about. That way you can know exactly what swing is
doing.
First prize would be to found out how the swing code dispatches the
mouse clicks on the table and sends them to the editor for that cell. A
break point on the editor while running in the debugger might help in
this regard.
good luck!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
-~----------~----~----~----~------~----~------~--~---