Well there is one more way....I prefer this, as its much simple!

JTextField cellEditorTextField = new JTextField(); // have your own
textfield for cell editor

DefaultCellEditor tableCellEditor = new
DefaultCellEditor(cellEditorTextField);

FocusAdapter focusAdapter = new FocusAdapter() { // write a focus listener
that trigger the stopCellEditing()
    public void focusLost(FocusEvent e) {
        myTable.getCellEditor(myTable.getEditingRow(),
myTable.getEditingColumn()).stopCellEditing();
    }
};

cellEditorTextField.addFocusListener(focusAdapter);

tableColumnModel.getColumn(1).setCellEditor(tableCellEditor);

By doing this, when "OK" is pressed, incase your TextField was the focus
owner will get a focus lost event
and everything will work fine.

regards,
Ramana

Wish you all a Happy New Year!


----- Original Message -----
From: "Sachin Hejip" <[EMAIL PROTECTED]>
To: "Peter Peterson" <[EMAIL PROTECTED]>; "Anand Hariharan"
<[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, December 24, 2002 9:52 AM
Subject: Re: JTable editing problem...


> I see what you mean - the problem is that I should have been clearer in my
> reply :-).
>
> I meant that you should add that code snippet in the OK Button's
> ActionListener. Later, I did notice your post to Anand about the dialog
not
> knowing whether the panel has a table or not. I would suggest that you
> create a panel that conforms to some interface say Editable. In the
> interface has an acceptEditedValues() method and let the panel decide what
> to call - that snippet if it has a table or nothing if not needed. The
> dialog will now work only with Editable panels and in the Ok Button's
> actionListener you can call editablePanel.acceptEditedValues().
>
> Ofcourse, this is not a Swing specific solution but in the light of all
the
> issues you are facing do you think this might be the cleaner solution?
>
> Regards
> Sachin
>
> ----- Original Message -----
> From: "Peter Peterson" <[EMAIL PROTECTED]>
> To: "Sachin Hejip" <[EMAIL PROTECTED]>; "Anand Hariharan"
> <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
> Sent: Tuesday, December 24, 2002 12:05 AM
> Subject: Re: JTable editing problem...
>
>
> > Sachin,
> > I think you missed my point. If user clicks on OK, then the dialog will
> > go away and stopCellEditing will never get called. This problem
> > is because of a bug in focus management as pointed by someone in
> > this forum
> >
> > rgds,
> > Peter
> >
> > --- Sachin Hejip <[EMAIL PROTECTED]> wrote:
> > > Yep, it saves it.
> > > stopCellEditing() accepts the value entered by the user.
> > >
> > > Regards
> > > Sachin
> > > ----- Original Message -----
> > > From: "Peter Peterson" <[EMAIL PROTECTED]>
> > > To: "Sachin Hejip" <[EMAIL PROTECTED]>; "Anand Hariharan"
> > > <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
> > > Sent: Saturday, December 21, 2002 2:59 AM
> > > Subject: Re: JTable editing problem...
> > >
> > >
> > > > Sachin,
> > > >
> > > > I did this. The problem I have here is what if user has entered
> > > > a correct value in the cell and then clicked  on OK button ?
> > > > In this case he is expecting that the value would be saved
> > > >
> > > > - Peter
> > > >
> > > > --- Sachin Hejip <[EMAIL PROTECTED]> wrote:
> > > > > The following snippet should solve the problem -
> > > > >
> > > > >                         int editingColumn =
> > > table.getEditingColumn();
> > > > >                         TableCellEditor defaultEditor =
> > > > >
> > > >
> > >
> > table.getDefaultEditor(table.getModel().getColumnClass(editingColumn));
> > > > >                         defaultEditor.stopCellEditing();
> > > > >
> > > > >
> > > > > If you have set editors on the table column then use the
> > > columnModel
> > > > > to get
> > > > > the TableColumn.
> > > > >
> > > > > Hope this helps.
> > > > >
> > > > > Regards
> > > > > Sachin
> > > > >
> > > > >
> > > > > ----- Original Message -----
> > > > > From: "Peter Peterson" <[EMAIL PROTECTED]>
> > > > > To: "Anand Hariharan" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
> > > > > Sent: Friday, December 20, 2002 7:14 AM
> > > > > Subject: Re: JTable editing problem...
> > > > >
> > > > >
> > > > > > Anand,
> > > > > >
> > > > > > I should have given more information. The dialog has a panel
> > > which
> > > > > > has different data editors depending on how it is invoked. In
> > > some
> > > > > > cases, the panel may not even have a table. In short, dialog
> > > > > doesnot
> > > > > > have an idea of what is being edited. In some cases, its
> > > JTable,
> > > > > > in some cases its just a JTextField or it could anything else.
> > > > > >
> > > > > > If I make OK button to find out what it is, I am making dialog
> > > > > > sensitive to data editor instance which I wanted to avoid
> > > because
> > > > > > its a not a good bean design pattern.
> > > > > >
> > > > > > let me know if there is any other way out.
> > > > > >
> > > > > > Also I have noticed that if I set a cell editor on a table,
> > > then
> > > > > > I can start editing cell, either by single clicking or double
> > > > > clicking.
> > > > > > BUUUT...surprisngly enough ONLY when I double click, I see my
> > > > > > JTextField
> > > > > > table cell renderer otherwise for single click JTable renders
> > > > > something
> > > > > > else than what I set !!!!!!!!!!!! ( My cell renderer which is a
> > > > > > JTextField has some properties set so that it looks good when
> > > user
> > > > > > starts editing )
> > > > > >
> > > > > > Any idea whats going on here ?
> > > > > >
> > > > > > rgds
> > > > > > Peter
> > > > > >
> > > > > >
> > > > > > --- Anand Hariharan <[EMAIL PROTECTED]> wrote:
> > > > > > > Hi Peter :
> > > > > > >
> > > > > > > We had the same problem and this is how we handle it :
> > > > > > >
> > > > > > > Consider m_EditTable as the table that is being currently
> > > edited.
> > > > > > > Execute this code when OK is clicked.
> > > > > > >
> > > > > > >
> > > > > > > if(m_Table.isEditing())
> > > > > > > {
> > > > > > >     Component c = m_Table.getEditorComponent();
> > > > > > >     if(c != null)
> > > > > > >     {
> > > > > > >         String val = "";
> > > > > > >         if(c instanceof JTextField)
> > > > > > >             val = ((JTextField)c).getText();
> > > > > > >         /*else if( c instanceof SomeOtherComponent )
> > > > > > >             val = (String) GetStringValueFromComponent;*/
> > > > > > >         m_Table.getModel().setValueAt (val,
> > > > > m_Table.getEditingRow(),
> > > > > > > m_Table.getEditingColumn());
> > > > > > >
> > > > > > >         m_Table.removeEditor();
> > > > > > >     }
> > > > > > > }
> > > > > > >
> > > > > > > Regards,
> > > > > > > _anand
> > > > > > > [EMAIL PROTECTED]
> > > > > > >
> > > > > > > http://www.javareference.com (Articles/Examples and more !)
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > ----- Original Message -----
> > > > > > > From: "Peter Peterson" <[EMAIL PROTECTED]>
> > > > > > > To: <[EMAIL PROTECTED]>
> > > > > > > Sent: Thursday, December 19, 2002 9:41 AM
> > > > > > > Subject: JTable editing problem...
> > > > > > >
> > > > > > >
> > > > > > > > Hi,
> > > > > > > >
> > > > > > > > I have a JDialog which has OK/Cancel button. When user
> > > clicks
> > > > > > > > on any of these buttons, dialog goes away (
> > > setVisible(false)
> > > > > ).
> > > > > > > >
> > > > > > > > This dialog has an editable JTable. I am using a JTextField
> > > > > > > > as a cell editor because I wanted to capture focusLost
> > > event
> > > > > > > > on a cell.
> > > > > > > >
> > > > > > > > Now the problem is that AFTER entering value in any cell
> > > > > > > > AND WITHOUT hiting Enter/Tab, if user clicks on OK/Cancel
> > > > > button,
> > > > > > > > the focus lost event is not getting called consitantly.
> > > Looks
> > > > > like
> > > > > > > > sometime, actionListener on ok/cancel buttons kicks in and
> > > > > > > sometimes
> > > > > > > > focusListener on cell kicks in. ( Note that to save new
> > > entered
> > > > > > > value
> > > > > > > > in table model, user must either hit enter or tab out so
> > > that
> > > > > > > > setValueAt gets called. )
> > > > > > > >
> > > > > > > > does any one know how to handle this properly ?
> > > > > > > >
> > > > > > > >
> > > > > > > > rgds,
> > > > > > > > Peter
> > > > > > > >
> > > > > > > >
> > > > > > > > __________________________________________________
> > > > > > > > Do you Yahoo!?
> > > > > > > > New DSL Internet Access from SBC & Yahoo!
> > > > > > > > http://sbc.yahoo.com
> > > > > > > > _______________________________________________
> > > > > > > > Swing mailing list
> > > > > > > > [EMAIL PROTECTED]
> > > > > > > > http://eos.dk/mailman/listinfo/swing
> > > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > > > __________________________________________________
> > > > > > Do you Yahoo!?
> > > > > > New DSL Internet Access from SBC & Yahoo!
> > > > > > http://sbc.yahoo.com
> > > > > > _______________________________________________
> > > > > > Swing mailing list
> > > > > > [EMAIL PROTECTED]
> > > > > > http://eos.dk/mailman/listinfo/swing
> > > > >
> > > > >
> > > >
> > > >
> > > > __________________________________________________
> > > > Do you Yahoo!?
> > > > New DSL Internet Access from SBC & Yahoo!
> > > > http://sbc.yahoo.com
> > > > _______________________________________________
> > > > Swing mailing list
> > > > [EMAIL PROTECTED]
> > > > http://eos.dk/mailman/listinfo/swing
> > >
> > >
> > >
> > === message truncated ===
> >
> >
> > __________________________________________________
> > Do you Yahoo!?
> > New DSL Internet Access from SBC & Yahoo!
> > http://sbc.yahoo.com
>
>
> _______________________________________________
> Swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/swing


_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to