Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

insertion management in DefaultCellEditor of a JTable

Thread view: 
oulan bator - 19 Dec 2005 16:34 GMT
Hi all,

I've got a JTable, with some editable cells using the "standard" Editor
(Column type is Double )

when I select a cell, and type "3" it add this figure at the end of the
cell value (lets says 1.0 for instance) and I bloody get 1.03 instead
of the expected "3" (my reference is Excel, you can try it)

How can I get this behaviour ? do I have to program my own editor ?

thanks in advance
chsadaki@hotmail.com - 19 Dec 2005 16:43 GMT
Hi ,
I worked with JTable before, but i didn't understand ur problem very
well,
do u mean that u enter a value and u get another one??
If u can send ur code maybe I can understand better ur problem and help
u

Shameram
oulan bator - 19 Dec 2005 16:51 GMT
hi,

there is no code, it works with every JTable.
1- Just set the type of a column to Double.
2- run your app, and select a single cell (let says it contains 1.0)
3- Type 3 on the keyboard
== the cell is being edited, and the value is currently 1.03
I'd rather see 3 (1.0 should be delete : in other words type sould
replace selection (that's the way Excel operates).
Dag Sunde - 20 Dec 2005 07:42 GMT
> hi,
>
[quoted text clipped - 5 lines]
> I'd rather see 3 (1.0 should be delete : in other words type sould
> replace selection (that's the way Excel operates).

One way to do it is to override JTable's editCellAt(...) method:

 public boolean editCellAt(int row, int column, EventObject e) {
   boolean res = super.editCellAt(row, column, e);
   Component c = this.getEditorComponent();
   if (c instanceof JTextComponent) {
     ( (JTextField) c).selectAll();
   }

   return res;
 }

This will select the old content the instant you start editing the cell,
with the result that it is immediately overwritten with the new content
your typing.

Signature

Dag.

oulan bator - 20 Dec 2005 08:18 GMT
Thanks a lot, it works fine, and overall, it's really smart !
oulan bator - 20 Dec 2005 08:34 GMT
one detail for others readers: I've change a litlle bit his code :
if (c instanceof JTextField) {
     ( (JTextField) c).selectAll();

it's more secure
Dag Sunde - 20 Dec 2005 08:35 GMT
> Thanks a lot, it works fine, and overall, it's really smart !

Just remember that it will stop working for a column if you
introduce a custom cell-editor that is based on something
else than JTextComponent...

Signature

Dag.

oulan bator - 20 Dec 2005 08:50 GMT
dag:
yes you are right, I think JTextComponent is even better.
For other customs editors, the "bug" does not apply ...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.