Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[JTable] problems having focus lost events (stop cell editing)

23 views
Skip to first unread message

Danny Fürniß

unread,
Feb 25, 2002, 2:33:38 AM2/25/02
to
Hello.

I want that my subclassed JTable-class to stop cell editing and clear the
selection if the user clicks outside the table. I think it should be done in
the focusLost of a FocusListener?! Therefore i've written the following
method:

public void tableFocusLostEvent(FocusEvent e, boolean bStop)
{
if (GDebug.debug) GDebug.out(CID, 1, "tableFocusLostEvent triggered");
TableCellEditor tce = getCellEditor();
if (bStop == true)
if (tce != null) tce.stopCellEditing();

getSelectionModel().clearSelection();

}//tableFocusLostEvent

...and within my subclass of JTable, at initialization-time i set up the
following focusListener:

addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
tableFocusLostEvent(e, true); //true means stop cell editing
}//focusLost
});

Now there are some problems:

1) Cell editing is stopped, but the last selected row stays selected. I use
SINGLE_SELECTION what means only complete rows are selectable.

2) Everytime i navigate by mouse WITHIN the table, the table fires an
focusLost event and cell editing is stopped. (after it was started through
the mouse click) :-( How can i determine that the table surely loses the
focus?

3) If i programmatically start cell editing at a specific cell the same
behaviour as in (2) happens. Seems as if starting an own cell editor forces
a table-focusLost event?!

Help on fixing these problems would be much appreciated.

Yours,
Danny Fürniß


Babu Kalakrishnan

unread,
Feb 26, 2002, 2:54:33 AM2/26/02
to
On Mon, 25 Feb 2002 08:33:38 +0100, Danny Fürniß <dfue...@debis.com> wrote:

>I want that my subclassed JTable-class to stop cell editing and clear the
>selection if the user clicks outside the table. I think it should be done in
>the focusLost of a FocusListener?! Therefore i've written the following
>method:

Trying to play around with Focus events on an editable JTable can be
pretty tricky. Your problems stem from the fact that the JTable loses
focus to the CellEditor component when you click inside an editable cell
- but you really want the actions to occur only if focus is lost to a
component outside the JTable. As far as I know, there is no
straightforward way of determining from inside a Focus listener as to
which component is going to get the focus next.

There are a couple of directions in which you might want to look for
solutions to the problem. If your intent is to ensure that the data in
the model for a cell that is being edited is updated when the user
clicks outside the table (It doesn't happen automatically now), you
could try to listen for FocusEvents in the CellEditor. But as of JDK 1.3
(I don't know if this has been fixed in 1.4) even this is not foolproof
because a CellEditor can be active even without it ever getting the
focus. The only foolproof way I have seen of updating the model data is
to listen for model level events in the CellEditor (like Document events
for TextFields) and update the model as and when the editor's model
changes.

Another hack one could think of (untested) is to start off a delayed
processing method from the FocusLost handler using the
SwingUtilities.invokeLater wrapper. (This will be processed only in the
next cycle of the EDT and hopefully the focus destination can be
determined by then). This method can then examine which component has
the focus currently - and if it happens to be not anything inside the
table, you could do the necessary processing.

BK

Danny Fürniß

unread,
Feb 26, 2002, 5:43:42 AM2/26/02
to
Hi!

Thank you for your response.


"Babu Kalakrishnan" <ka...@sankya.com> schrieb im Newsbeitrag
news:slrna7mfp...@ganga.sankya.com...


> Trying to play around with Focus events on an editable JTable can be
> pretty tricky. Your problems stem from the fact that the JTable loses
> focus to the CellEditor component when you click inside an editable cell
> - but you really want the actions to occur only if focus is lost to a
> component outside the JTable. As far as I know, there is no
> straightforward way of determining from inside a Focus listener as to
> which component is going to get the focus next.

I see.


> There are a couple of directions in which you might want to look for
> solutions to the problem. If your intent is to ensure that the data in
> the model for a cell that is being edited is updated when the user
> clicks outside the table (It doesn't happen automatically now), you

Yes, thats what i want. But i want to deselect the table's selection, too.

> could try to listen for FocusEvents in the CellEditor. But as of JDK 1.3
> (I don't know if this has been fixed in 1.4) even this is not foolproof

I'm using JDK 1.2.2 and i cannot upgrade to a newer one since i'm developing
in a customer-dependent framework.

> because a CellEditor can be active even without it ever getting the
> focus. The only foolproof way I have seen of updating the model data is
> to listen for model level events in the CellEditor (like Document events

Could someone provide me more information on this _model level events_ ?
Or could someone point me to resources on the web where i can find such
information?

> Another hack one could think of (untested) is to start off a delayed
> processing method from the FocusLost handler using the
> SwingUtilities.invokeLater wrapper. (This will be processed only in the
> next cycle of the EDT and hopefully the focus destination can be
> determined by then). This method can then examine which component has
> the focus currently - and if it happens to be not anything inside the
> table, you could do the necessary processing.

I don't understand. :-( How is it possible to examine which component
currently has the focus? Is there a static FocusManager around which i can
_ask_? And on which component should i register the FocusListener to perform
the focusLost Events (JTable or the CellEditors or what)?

I've got another idea but i'm not sure if this can do the job and even if
this is possible:
I thought of having a 'JGlassPane' (i think there is a component around,
called so) which is placed over all my other components (is this possible?)
and then recognize mouseclicks which don't match with the area of the
underlaying JTable. What do you think?


Yours,
Danny Fürniß

Linda Radecke

unread,
Feb 26, 2002, 7:11:38 AM2/26/02
to

"Danny Fürniß" wrote:

> "Babu Kalakrishnan" <ka...@sankya.com> schrieb im Newsbeitrag

[...]

> > Another hack one could think of (untested) is to start off a delayed
> > processing method from the FocusLost handler using the
> > SwingUtilities.invokeLater wrapper. (This will be processed only in the
> > next cycle of the EDT and hopefully the focus destination can be
> > determined by then). This method can then examine which component has
> > the focus currently - and if it happens to be not anything inside the
> > table, you could do the necessary processing.

> I don't understand. :-( How is it possible to examine which component
> currently has the focus?

Just answering to *this* part of your question:
The class SwingUtilities has the method findFocusOwner(Component c),
which returns the child component which has the focus. Since 1.4 this
is deprecated and replaced by getFocusOwner() in KeyboardFocusManager.
(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner())
Component also has the method hasFocus(), which returns true if this
component is the focus owner (since 1.2). In 1.4 this is replaced by
isFocusOwner().

Linda
--
(=)
/ li...@jalice.ch - http://www.jalice.net
(=)
/ l.ra...@hswzfh.ch - http://www.hswzfh.ch
(=)


Babu Kalakrishnan

unread,
Feb 26, 2002, 10:03:33 AM2/26/02
to
On Tue, 26 Feb 2002 11:43:42 +0100, Danny Fürniß <dfue...@debis.com> wrote:

>> There are a couple of directions in which you might want to look for
>> solutions to the problem. If your intent is to ensure that the data in
>> the model for a cell that is being edited is updated when the user
>> clicks outside the table (It doesn't happen automatically now), you
>
>Yes, thats what i want. But i want to deselect the table's selection, too.

Which means that you have to figure out if the JTable has lost focus and
not to any component internal to it - that's the tricky part.


>> could try to listen for FocusEvents in the CellEditor. But as of JDK 1.3
>> (I don't know if this has been fixed in 1.4) even this is not foolproof
>
>I'm using JDK 1.2.2 and i cannot upgrade to a newer one since i'm developing
>in a customer-dependent framework.
>
>> because a CellEditor can be active even without it ever getting the
>> focus. The only foolproof way I have seen of updating the model data is
>> to listen for model level events in the CellEditor (like Document events
>
>Could someone provide me more information on this _model level events_ ?
>Or could someone point me to resources on the web where i can find such
>information?

If your editor is a JTextField, you register a DocumentListener on it
using the addDocumentListener method and you'll be notified whenever the
document (that holds your data being edited) has undergone any mutation.

If it is a checkbox, you could register an addItemListener to it to
detect when it changes state.


>> Another hack one could think of (untested) is to start off a delayed
>> processing method from the FocusLost handler using the
>> SwingUtilities.invokeLater wrapper. (This will be processed only in the
>> next cycle of the EDT and hopefully the focus destination can be
>> determined by then). This method can then examine which component has
>> the focus currently - and if it happens to be not anything inside the
>> table, you could do the necessary processing.

>I don't understand. :-( How is it possible to examine which component
>currently has the focus? Is there a static FocusManager around which i can
>_ask_? And on which component should i register the FocusListener to perform
>the focusLost Events (JTable or the CellEditors or what)?

Check Linda's response for how to detect the current Focus Owner.
Basically you will need to register the focusListener on the JTable and
all your CellEditors, and do something like this in your FocusLost
handler :

void focusLost(FocusEvent e)
{
if (!e.isTemporary()) // Ignore temporary focusLost events
{
SwingUtilities.invokeLater(new Runnable()
{ // This delays the processing to the next
// EDT cycle
public void run()
{
tableFocusLost(e);
}
}
}
}


void tableFocusLost(FocusEvent e)
{
// Find out who has focus now
// If it is the JTable or a CellEditor, exit without doing anything

// Else do your processing
}


BK

Danny Fürniß

unread,
Feb 27, 2002, 2:28:59 AM2/27/02
to
Hi Babu!
Thank you for your response! Sounds great and working. I'll try that today!


"Babu Kalakrishnan" <ka...@sankya.com> schrieb im Newsbeitrag

news:slrna7n8u...@ganga.sankya.com...


> On Tue, 26 Feb 2002 11:43:42 +0100, Danny Fürniß <dfue...@debis.com>
wrote:

> If your editor is a JTextField, you register a DocumentListener on it
> using the addDocumentListener method and you'll be notified whenever the
> document (that holds your data being edited) has undergone any mutation.
>
> If it is a checkbox, you could register an addItemListener to it to
> detect when it changes state.

Could i do so if its a ComboBox, too?

I only use JTextFields and JComboBoxes as CellRenderer.
My JTextFields use a own Document extending PlainDocument to valida the user
input.

> Check Linda's response for how to detect the current Focus Owner.
> Basically you will need to register the focusListener on the JTable and
> all your CellEditors, and do something like this in your FocusLost
> handler :
>
> void focusLost(FocusEvent e)
> {
> if (!e.isTemporary()) // Ignore temporary focusLost events
> {
> SwingUtilities.invokeLater(new Runnable()
> { // This delays the processing to the next
> // EDT cycle
> public void run()
> {
> tableFocusLost(e);
> }
> }
> }
> }
>
>
> void tableFocusLost(FocusEvent e)
> {
> // Find out who has focus now
> // If it is the JTable or a CellEditor, exit without doing anything
>
> // Else do your processing
> }

Thanks for giving me that structure of code. Now i understand the problem
and try to fix it...

Yours,
Danny

Danny Fürniß

unread,
Feb 27, 2002, 9:27:27 AM2/27/02
to
Hi all!

I've written a class TableFocusLostManager implementing FocusListener that
should do the job for me, but something went wrong.
I pass a reference to my table. A Vector collects all TableCellEditors for
later asking if their components have focus, etc.
I have only one column where i use ComboBoxes as editors and there seems to
be my problem...
Every row has a comboboxeditor instance for its own. The rest of the columns
(every column) have only one textFieldeditor for the whole column.
If i click around (not on the _ComboBox-column_) within the table it
interacts correctly and if i click outside the table into other components
cell editing is stopped and the selection disappears as i want.
But if i click onto the combobox-column cell editing is stopped too and that
shouldn't behave this way. It seems as i need to register something (a
component?) that is held by the ComboBoxCellEditor (which handles different
_ComboBox-DefaultCellEditors_ for each row) to my TableFocusLostManager, but
i don't know what!?
Any suggestions?
I tried registering the ComboBox instances i pass to the DefaultCellEditor
but that don't work.


I would be pleased if someone could help me fix this problem.

Yours,
Danny

Here is the code:

/**
* This class implements the FocusListener interface to be
* able to determine when the JTable loses focus and
* then stop cell editing within the table.
* This task is a bit tricky as there are some problems around.
* It has to figure out if the JTable has lost focus to components
* outside and not to any component internal to it.
* Therefore it has to determine which of the managed
* objects has the focus and if none of them has it stop cell editing.
* //DF
*/
public class TableFocusLostManager implements FocusListener
{

public static final String CID = "TableFocusLostManager";
private JTable table = null;
private Vector editors = new Vector();

/**
* Constructs the TableFocusLostManager instance
* @param tbl the JTable
*/
public TableFocusLostManager(JTable tbl)
{
table = tbl;
table.addFocusListener(this);
}//TableFocusLostManager

/**
* The action method if a component gains the focus
* @param e the FocusEvent
*/
public void focusGained(FocusEvent e)
{
//don't care
}//focusGained

/**
* The action method if a component loses the focus
* @param e the FocusEvent
*/
public void focusLost(FocusEvent e)
{
if (GDebug.debug) GDebug.out(CID, 1, "focusLost was triggered");
if (GDebug.debug) GDebug.out(CID, 1, ("\n\tSOURCE: " +
e.getSource().toString()));
final FocusEvent fe = e;


if (!e.isTemporary()) // Ignore temporary focusLost events
{
SwingUtilities.invokeLater(new Runnable() // This delays the

processing to the next EDT cycle
{
public void run()
{
tableFocusLost(fe);
}//run
});//invokeLater
}//if
}//focusLost

/**
* Processes stopping the cell editor if no one of the
* observed objects has the focus.
* @param e the initial FocusEvent
*/
private synchronized void tableFocusLost(FocusEvent e)
{
if (GDebug.debug) GDebug.out(CID, 1, "Entering tableFocusLost");
//determine if any of the observed objects has focus
if (table.hasFocus()) return;

TableCellEditor editor = null;

for (int i = 0; i < editors.size(); ++i)
{
editor = (TableCellEditor)editors.elementAt(i);

if (editor != null)
{
if (editor instanceof TextFieldCellEditor)
{
if (GDebug.debug) GDebug.out(CID, 1, "\tTextFieldCellEditor found
(" + i + ")");
if
(((TextFieldCellEditor)(editor)).getEditorComponentAt(0).hasFocus() == true)
return;
if (GDebug.debug) GDebug.out(CID, 1, "\t\t-> had not the focus");
}//if
else if (editor instanceof ComboBoxCellEditor)
{
if (GDebug.debug) GDebug.out(CID, 1, "\tComboBoxCellEditor found
(" + i + ")");
if
(((ComboBoxCellEditor)(editor)).getEditorComponentAt(0).hasFocus() == true)
return;
if (GDebug.debug) GDebug.out(CID, 1, "\t\t-> had not the focus");
}//if else
}//if
else
{
if (GDebug.debug) GDebug.out(CID, 1, "tableFocusLost -> editor is
null");
}//else
}//for

//stop cell editing now!
if (GDebug.debug) GDebug.out(CID, 1, "\tStop Cell Editing now...");
TableCellEditor tce = table.getCellEditor();


if (tce != null) tce.stopCellEditing();

table.getSelectionModel().clearSelection();
if (GDebug.debug) GDebug.out(CID, 1, "Exiting tableFocusLost");
}//tableFocusLost


/**
* Adds a TableCellEditor to the observed CellEditor list
* of this TableFocusLostManager.
* Do only add TextFieldCellEditors or ComboBoxCellEditors
* which can be found in this package!
* @param textField_or_comboBox_celleditor the TableCellEditor
*/
public void addCellEditor(TableCellEditor
textField_or_comboBox_celleditor)
{
TableCellEditor editor = textField_or_comboBox_celleditor;
if (editor instanceof TextFieldCellEditor)
{

((TextFieldCellEditor)(editor)).getEditorComponentAt(0).addFocusListener(thi
s);
}//if
else if (editor instanceof ComboBoxCellEditor)
{
((ComboBoxCellEditor)(editor)).addFocusListener(this);
}//if else

editors.addElement(editor);
}//addCellEditor


}//end of class TableFocusLostManager

################### and the class ComboBoxCellEditor
###########################
/**
* An editor managing different editors for every row.
*/
public class ComboBoxCellEditor implements TableCellEditor
{
protected Hashtable editors;
protected TableCellEditor editor, defaultEditor;
JTable table;
private FocusListener focuslistener;
private JComboBox combo = null;

/**
* Constructs a new ComboBoxCellEditor instance and
* creates a default editor using a disabled JComboBox.
*
* @param table the table in which this instance is used
* @see TableCellEditor
* @see DefaultCellEditor
*/
public ComboBoxCellEditor(JTable table)
{
this.table = table;
editors = new Hashtable();
combo = new JComboBox();
combo.setEnabled(false);
defaultEditor = new DefaultCellEditor(combo);
}//ComboBoxCellEditor

/**
* Sets the specified CellEditor at the given row index in this
* 'column'-editor-manager class.
* @param row the corresponding table row
* @param editor the table cell editor
*/
public void setEditorAt(int row, TableCellEditor editor)
{
editor.getTableCellEditorComponent(null, null, false, 0,
0).addFocusListener(focuslistener);
editors.put(new Integer(row),editor);
}//setEditorAt

/**
* Returns the EditorComponent of the TableCellEditor at
* the given row-index.
* @param row the row-index which editors component is needed
* @return the tablecelleditors rendering-component
*/
public Component getEditorComponentAt(int row)
{
editor = (TableCellEditor)editors.get(new Integer(row));

if (editor == null)
{
editor = defaultEditor;
}//if

return ((DefaultCellEditor)editor).getComponent();
}//getEditorComponentAt

/**
* See Superclass TableCellEditor for more information on
* this method, its just overriden.
* The TableCellEditorComponent of the CellEditor in the
* given row is fetched and returned by this method.
*/
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column)
{

editor = (TableCellEditor)editors.get(new Integer(row));

if (editor == null)
{
editor = defaultEditor;
}//if

return editor.getTableCellEditorComponent(table, value, isSelected, row,
column);
}//getTableCellEditorComponent

/**
* Returns the value of the current editor.
* @return the value of the current editor
*/
public Object getCellEditorValue()
{
return editor.getCellEditorValue();
}//getCellEditorValue

/**
* Stops cell editing.
* @returns true if cell editing was stopped successfully
*/
public boolean stopCellEditing()
{
return editor.stopCellEditing();
}//stopCellEditing

/**
* Cancels cell editing.
*/
public void cancelCellEditing()
{
editor.cancelCellEditing();
}//cancelCellEditing

/**
* Checks if the inquired Cell is editable.
* @param anEvent the event that forces this method be called
* @return true if the cell is editable, else return false
*/
public boolean isCellEditable(EventObject anEvent)
{
selectEditor((MouseEvent)anEvent);
return editor.isCellEditable(anEvent);
}//isCellEditable

/**
* Adds an CellEditorListener to the current editor.
* @param l the Listener that should be added to the current editor
*/
public void addCellEditorListener(CellEditorListener l)
{
editor.addCellEditorListener(l);
}//addCellEditorListener

/**
* Removes the given CellEditorListener from the current
* editor.
* @param l the Listener that should be removed
*/
public void removeCellEditorListener(CellEditorListener l)
{
editor.removeCellEditorListener(l);
}//removeCellEditorListener

/**
* Checks if the inquired cell should be selected.
* @param anEvent the Event that inquires the cell.
* @return true if the cell should be selected, else return false
*/
public boolean shouldSelectCell(EventObject anEvent)
{
selectEditor((MouseEvent)anEvent);
return editor.shouldSelectCell(anEvent);
}//shouldSelectCell

/**
* Selects the editor of the cell where the mouseevent occured.
* @param e the MouseEvent that was pointed on a table cell
*/
protected void selectEditor(MouseEvent e)
{
int row;
if (e == null)
{
row = table.getSelectionModel().getAnchorSelectionIndex();
}//if
else
{
row = table.rowAtPoint(e.getPoint());
}//else
editor = (TableCellEditor)editors.get(new Integer(row));
if (editor == null)
{
editor = defaultEditor;
}//if
}//selectEditor

/**
* Adds a FocusListener to this TableCellEditor-Wrapper class.
* Each Combobox has to get this focuslistener too.
* @param listener the FocusListener
*/
public void addFocusListener(FocusListener listener)
{
focuslistener = listener;
combo.addFocusListener(listener);
}//focuslistener

}//end of class ComboBoxCellEditor


Danny Fürniß

unread,
Feb 27, 2002, 9:42:33 AM2/27/02
to
Ooh, is it possible that a disabled combobox never gains focus???
I mean asking:

mydisabledComboBox.hasFocus()

Is it possible that this call returns true?

I use disabled JComboBoxes by default in my ComboBoxCellEditor.

Depending on the values entered in the two columns left to the one using the
_cb-mechanism_
i set up the CellEditors from outside.

Such a setting is done by:

/**
* Sets a combo box as the editor for a table cell
* in the column customer's choice.
*
* @param row, the affected row
* @param cb, the combo box which is used as the CellEditor
*/
private void setupRowEditor(int row, StdComboBox cb)
{
ccEditor.setEditorAt(row, new DefaultCellEditor(cb));
}//setupRowEditor


Where ccEditor is an instance of ComboBoxCellEditor and StdComboBox is a
subclass of JComboBox. Don't ask for why i use own ComboBoxes... it's a
framework i have to deal with ;-(


Yours,
Danny


Babu Kalakrishnan

unread,
Feb 27, 2002, 12:32:40 PM2/27/02
to
On Wed, 27 Feb 2002 15:27:27 +0100, Danny Fürniß <dfue...@debis.com> wrote:
>
>I've written a class TableFocusLostManager implementing FocusListener that
>should do the job for me, but something went wrong.
>I pass a reference to my table. A Vector collects all TableCellEditors for
>later asking if their components have focus, etc.
>I have only one column where i use ComboBoxes as editors and there seems to
>be my problem...
>Every row has a comboboxeditor instance for its own. The rest of the columns
>(every column) have only one textFieldeditor for the whole column.
>If i click around (not on the _ComboBox-column_) within the table it
>interacts correctly and if i click outside the table into other components
>cell editing is stopped and the selection disappears as i want.
>But if i click onto the combobox-column cell editing is stopped too and that
>shouldn't behave this way. It seems as i need to register something (a
>component?) that is held by the ComboBoxCellEditor (which handles different
>_ComboBox-DefaultCellEditors_ for each row) to my TableFocusLostManager, but
>i don't know what!?
>Any suggestions?
>I tried registering the ComboBox instances i pass to the DefaultCellEditor
>but that don't work.
>
>
>I would be pleased if someone could help me fix this problem.

[code snipped]

I think you're overdoing it a bit by trying to test every individual
editor that can be invoked by the table.

The editor component can have focus only if table.isEditing() returns
true. And in this case table.getEditorComponent() should return the
editor component that is currently active. So you essentially need to
check only if that particular component has focus.

Comboboxes pose a problem here because they are not simple components,
but contain several sub-components within it. So it's almost certain to
return false if you query it for hasFocus(). So one needs to query its
sub-components to check if any one of them have focus.

I don't have the API docs or source code at hand now - so not sure how
one should go about it. But off the top of the hat, you might want to
try checking isAncestorOf(currentlyFocusedComponent) on the editor if it
happens to be a JComboBox (where "currentlyFocusedComponent" is the one
you that has the focus - you can determine it by using the
SwingUtilities method). If this returns true, then the component in
question is within the JComboBox and so within the table.

BK


Danny Fürniß

unread,
Feb 28, 2002, 6:02:08 AM2/28/02
to
Hi Babu!

Thank you very much for spending your time on my probs.

"Babu Kalakrishnan" <ka...@sankya.com> schrieb im Newsbeitrag

news:slrna7q61...@ganga.sankya.com...


> The editor component can have focus only if table.isEditing() returns
> true. And in this case table.getEditorComponent() should return the
> editor component that is currently active. So you essentially need to
> check only if that particular component has focus.

I have tried the above and it works.

> Comboboxes pose a problem here because they are not simple components,
> but contain several sub-components within it. So it's almost certain to
> return false if you query it for hasFocus(). So one needs to query its
> sub-components to check if any one of them have focus.

Using the above mechanism don't cause the problem with the comboboxes...
YEAH!

Here is my code:

private synchronized void tableFocusLost(FocusEvent e)
{

if (table.hasFocus()) return;
if (table.isEditing() == true)
{
if (table.getEditorComponent().hasFocus() == true)
{
return;
}//if
}//if

//stop cell editing now!


TableCellEditor tce = table.getCellEditor();
if (tce != null) tce.stopCellEditing();
table.getSelectionModel().clearSelection();

}//tableFocusLost


Thanks again and have a nice time.

Yours,
Danny


Babu Kalakrishnan

unread,
Mar 1, 2002, 1:45:27 AM3/1/02
to
On Thu, 28 Feb 2002 12:02:08 +0100, Danny Fürniß <dfue...@debis.com> wrote:

>Thank you very much for spending your time on my probs.

You're welcome.

> private synchronized void tableFocusLost(FocusEvent e)
> {
> if (table.hasFocus()) return;
> if (table.isEditing() == true)
> {
> if (table.getEditorComponent().hasFocus() == true)
> {
> return;
> }//if
> }//if
>
> //stop cell editing now!
> TableCellEditor tce = table.getCellEditor();
> if (tce != null) tce.stopCellEditing();
> table.getSelectionModel().clearSelection();
> }//tableFocusLost

One small comment about coding style. for conditional expressions
involving boolean values, you don't need to specify "== true"
explicitly.

if (table.isEditing())
{

}

is good enough. (And similarly for the hasFocus()==true construct). And
if you need a "x==false" construct, use !x instead.

While there is nothing syntactically wrong with the "== true" usage, my
feeling is that it is a good practice to avoid it since it can lead to
some hard-to-trace bugs if the first expression happens to a variable
that is assignable (not so in the cases above) and you type in "="
instead of "==" (A very common error especially if you come from a
Pascal background) - and the compiler will not give you an error because
the conditional is a valid boolean expression.

boolean x;

if (x=true)
{
// Code here is always executed..
}

BK

0 new messages