Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JFormattedTextField in DefaultCellEditor focus problem

132 views
Skip to first unread message

Dave Spindle

unread,
Sep 18, 2002, 11:56:21 AM9/18/02
to
Hi there!

I am having trouble using this new Component in a DefaultCellEditor. If you
enter a value in the cell, and press RETURN, things work as you would expect
a custom JTextField cell to. However, if you enter the same value and click
elsewhere in the table (ie. focusLost), the current value is not kept but
rather it reverts to the original value. I took a look at the javadoc and
played with setFocusLostBehavior(int) for a while but just can't get it to
work. Below is my DefaultCellEditor. Can anyone help with this? I've
tried making my own focusAdapter to get the value manually but no luck.

Thanks in advance!

Dave

class NumberEditor2 extends DefaultCellEditor {
public NumberEditor2() {
super(new JCheckBox());
JFormattedTextField field =
new JFormattedTextField(new java.text.DecimalFormat("0"));
editorComponent = field;

setClickCountToStart(1);

//Must do this so that editing stops when appropriate.
field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});

field.setFocusLostBehavior(JFormattedTextField.COMMIT);

}

protected void fireEditingStopped() {
super.fireEditingStopped();
}

public Object getCellEditorValue() {
return new
Integer(((Number)((JFormattedTextField)editorComponent).getValue()).intValue
());
}

public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
((JFormattedTextField)editorComponent).setValue(value);
return editorComponent;
}
}


Dave Spindle

unread,
Sep 18, 2002, 1:44:01 PM9/18/02
to
OK, I figured it out. While debugging, I added a focusListener to the
component and found out that getCellEditorValue() was being called before
the value was being committed. All I had to do was
call((JFormattedTextField)editorComponent).commitEdit() before I fetched the
value.

Thanks to all who looked at the posting.

Dave


"Dave Spindle" <spindled...@cliff.isd.saic.cooooom> wrote in message
news:3d8840aa$1...@cpns1.saic.com...

Maurizio

unread,
Sep 18, 2002, 4:10:44 PM9/18/02
to

Dave Spindle <spindled...@cliff.isd.saic.cooooom> wrote in message
3d88...@cpns1.saic.com...
> > file://Must do this so that editing stops when appropriate.

> > field.addActionListener(new ActionListener() {
> > public void actionPerformed(ActionEvent e) {
> > fireEditingStopped();
> > }
> > });
> >
> > field.setFocusLostBehavior(JFormattedTextField.COMMIT);
> >
> > }
> >
> > protected void fireEditingStopped() {
> > super.fireEditingStopped();
> > }
> >
> > public Object getCellEditorValue() {
> > return new
> >
>
Integer(((Number)((JFormattedTextField)editorComponent).getValue()).intValue
> > ());
> > }
> >
> > public Component getTableCellEditorComponent(JTable table,
> > Object value,
> > boolean isSelected,
> > int row,
> > int column) {
> > ((JFormattedTextField)editorComponent).setValue(value);
> > return editorComponent;
> > }
> > }
> >
> >

When I have made an editable table, i haven't known there are
JFormattedTextField but i think this example
could be good also for you:

import javax.swing.*;
import java.awt.*;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.DefaultCellEditor;

public class DoubleEditor extends DefaultCellEditor {
private TxtNumero txtDouble;
** change TxtNumero with JFormattedTextField and try

public DoubleEditor(TxtNumero txtDouble) {
super(txtDouble);
this.txtDouble = txtDouble;
this.setClickCountToStart(1);
}
public Object getCellEditorValue() {
Double dbl = new Double("0");
try{
dbl = new Double(txtDouble.getText());
}
catch (NumberFormatException exc) {
Toolkit.getDefaultToolkit().beep();
}
return dbl;


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

txtDouble.setText(StringModifier02.espandiDouble(((Double)value).doubleValue
()));
return txtDouble;
}
}

/*where you call the editor */
private void setUpDoubleEditor(int column_index) {
TxtNumero dblField = new TxtNumero ****use your text field
DoubleEditor doubleEditor = new DoubleEditor(dblField);

table.getColumnModel().getColumn(column_index).setCellEditor(doubleEditor);
}

This example work from VM 1.3
>
>


0 new messages