Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Within the first afternoon I found 2 of the 3 needed solutions, and the 3rd came to me over the weekend!..."

Geography

Where in the world do Tek-Tips members come from?

JTable CellEditor not being called second edit

pointertowhat (Programmer)
4 Mar 09 8:54
I've got a small test program set up to try and resolve this problem but I'm at a loss now. I have a jTable that has two columns - each of type double.

The table uses a cell editor that selects all the contents on edit. (it also disables the editing on a mouse click to prevent masking of the problem)

CODE

public class DblCellEditor extends AbstractCellEditor implements TableCellEditor {
    JComponent component = new JTextField();
    // This method is called when editing is completed.
    // It must return the new value to be stored in the cell.
    public Object getCellEditorValue() {
        return ((JTextField)component).getText();
    }

    // This method is called when a cell value is edited by the user.
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        // Configure the component with the specified value
        ((JTextField)component).setText(String.valueOf(value));
        ((JTextField)component).selectAll();
        // Return the configured component
        System.out.println("I've been Called!!");
        return component;
    }

    @Override
    public boolean isCellEditable(EventObject evt) {
        if (evt instanceof MouseEvent) {
            return false;
        } else {
            return true;
        }
    }

}

The cells are initialised with 0, which when editing starts shows as 0.0

The first time you edit a selected cell (say by simply typing a number) the number you have typed overwrite the current cell value, so say we highlighted the cell and type 5 then <enter> the cell now has a value of 5. All good so far. Now if we go back and edit the cell again by reselecting it and typing 9 the new cell value is 5.09. The CellEditor's getTableCellEditorComponent was NOT called the second time we edit the cell. I've added a println statement to prove this.

As I was typing that I had a relevation. The cell editor returns the value as a string so next time we edit the cell the default editor is called.

So my question is now a tip - remember to return the correct type...

CODE

public Object getCellEditorValue() {
        String foo = ((JTextField)component).getText();
// you will need to check this doesn't throw an
// exception if foo isn't a valid double.
        double i_spent_hours_on_this = Double.valueOf(foo);
        return new Double(i_spent_hours_on_this);
    }


blush

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close