insertion management in DefaultCellEditor of a JTable

O

oulan bator

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
 
C

chsadaki

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
 
O

oulan bator

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).
 
D

Dag Sunde

oulan bator said:
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).

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.
 
O

oulan bator

one detail for others readers: I've change a litlle bit his code :
if (c instanceof JTextField) {
( (JTextField) c).selectAll();

it's more secure
 
D

Dag Sunde

oulan bator said:
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...
 
O

oulan bator

dag:
yes you are right, I think JTextComponent is even better.
For other customs editors, the "bug" does not apply ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top