Edit JTable cell particulars, how to avoid append value on single click?

I have a JTable, and column selection is allowed...

table.setColumnSelectionAllowed(true);

... and I have a custom model with implementations for isCellEditable() and setValueAt(), which do their things. setValueAt() also fires a row updated event for the current row. All of my getValueAt() calls return strings. There is really nothing special about this table otherwise.

Here is the behavior that I see. If I select a cell that has a value in it and I start to type, The cell goes back to its original color (not the selection color), and it is surrounded by a thicker line, and there is no cursor visible. The character that I just typed is appended to the old value. If I try to arrow over, the cell selection changes.

e.g. the old contents is "8%" I select the cell (single click), and type 5. The value that is passed to the model's setValueAt() method becomes "8%5".

What I would like to see happen, is when I begin to edit a cell in this fashion, that the original value be wiped out. I would also like to keep the behavior of double clicking the cell. Whereby the original contents are not wiped out, and I can use the arrow keys to edit the value.

Thanks.

[1211 byte] By [billrobertson42a] at [2007-11-27 9:22:19]
# 1

> and I have a custom model with implementations setValueAt(),

Why? Whats wrong with the DefaultTableModel?

> setValueAt() also fires a row updated event for the current row.

Why would you do this for an entire row, when only a single cell changes. Thats one reason to use the DefaultTableModel, it fires the correct events.

> What I would like to see happen, is when I begin to edit a cell in this fashion, that the original value be wiped out.

This posting shows my solution for this:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=640471

camickra at 2007-7-12 22:16:42 > top of Java-index,Desktop,Core GUI APIs...
# 2
I don't use the default table model because my data is in objects not arrays, and it fires the row updated event because editing one property affects others.Message was edited by: billrobertson42 -- spelling
billrobertson42a at 2007-7-12 22:16:42 > top of Java-index,Desktop,Core GUI APIs...
# 3

> objects note arrays,

Don't know what an "objects not arrays" is, but you can build an DefaultTableModel from an Array.

> editing one property affects others.

Then I would use a TableModelListener:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=566133

camickra at 2007-7-12 22:16:42 > top of Java-index,Desktop,Core GUI APIs...
# 4
The underlying data is in objects. The underlying data is neither in arrays nor lists. Therefore, I wrote a custom model. However, that has nothing to do with the question. The question is, how do I make the existing cell contents clear when the user starts typing?
billrobertson42a at 2007-7-12 22:16:42 > top of Java-index,Desktop,Core GUI APIs...
# 5

> However, that has nothing to do with the question.

Then why was it included as part of the problem description?

Maybe it does and maybe it doesn't. Many problems are caused by people unnecessarily creating their own TableModel without any special reason.

Anyway I already gave a solution in my first reply.

camickra at 2007-7-12 22:16:42 > top of Java-index,Desktop,Core GUI APIs...
# 6

I included all information because that's the right thing to do.

Anyway, I figured it out.

I extended DefaultCellEditor and overrode these two methods.

public boolean isCellEditable(EventObject anEvent) {

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

The first one calls super and then figures out if the editing is initiated via. mouse or keyboard. The second one acts on that information and either passes the "value" parameter up to the super call or an empty string. When the empty string is passed up to the super call it wipes the previous value of the cell.

The new cell editor has to be installed on each column in the table.

billrobertson42a at 2007-7-12 22:16:42 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I included all information because that's the right thing to do.

Including unecessary information detracts from question. The question is about editing not the TableModel used. By including information about the TableModel, the implication is that it works fine with the DefaultTableModel, but then causes a problem with your custom model. Thats why only information directly related to the problem should be included.

In fact the best way to post a question is to include a SSCCE so the quesion has been narrowed down to the smallest focus possible:

see http://homepage1.nifty.com/algafield/sscce.html,

camickra at 2007-7-12 22:16:42 > top of Java-index,Desktop,Core GUI APIs...