Article View: borland.public.jbuilder.java.swing
Article #8324Re: Subclassing DefaultCellEditor
From: John P. McGrath
Date: Wed, 20 Sep 2000 00:00
Date: Wed, 20 Sep 2000 00:00
39 lines
2071 bytes
2071 bytes
On Tue, 12 Sep 2000 06:01:53 -0400, John P. McGrath wrote:
> I have a JTextField based class that sets the selection when it receives
> the focus and clears the selection when it loses focus. This works
> pretty well for me.
>
> I have also used this class as the basis for a CellEditor and it works
> pretty well. There are some oddities due to the way that JTable gives
> focus to a CellEditor that I have not yet chased down, but it works well
> enough as is.
I just looked at that code and there is one other thing that I forgot to
mention. Doing the SelectAll() when in the focusGained() works fine for
the JTextField based component. But when I tried using this as the basis
for a cell editor, it did not work.
This is because of the way that the JTable re-targets the mouse event at
the editor component. Since JTable has to realize the TableCellEditor
component and then pass it the mouse event, this happens in a slightly
different order. The result is that the component selects all of the
text when it gains focus, and then it receives the mouse click which ends
up clearing the selection.
The answer is to wrap the selectAll() call in a Runnable and then use
SwingUtilities.invokeLater to run it. For example:
SwingUtilities.invokeLater( new Runnable() {
public void run() {
selectAll();
}
}
This causes the selectAll() to execute after the mouse event, so the
selection does not get cleared by the mouse event.
--
Regards,
John McGrath
Technical Details
Message-ID:
<MPG.1432263456bd1ca0989b5a@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<8p4u03$ohn11@bornews.borland.com> <MPG.1427a5a5672b2fb1989b20@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,3bf75799d0f156c1,start X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: Subclassing DefaultCellEditor Date: 2000/09/20 Message-ID: <MPG.1432263456bd1ca0989b5a@forums.borland.com> X-Deja-AN: 671937066 References: <8p4u03$ohn11@bornews.borland.com> <MPG.1427a5a5672b2fb1989b20@forums.borland.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing