P
Pawel Poplawski
I have encountered strange problem:
With class included below I define editor for JTable column.
JFormattedTextFiels behaves in confusing way: when I enter any string
that gives a chance to be interpreted as number formatter says that
everything is OK, and I can tolerate such a behaviour, but then garbage
input is not translated into number but keeps being displayed until I
reeneter the cell, than in almost magical way the value becomes properly
formatted - there are println's in code to show data flow, but there is
sth. behind the scenes what I do not understed.
I would greatly appreciate any help.
public class NumberCellEditor extends DefaultCellEditor
{
JFormattedTextField ftf;
DecimalFormat numberFormat;
private boolean DEBUG = false;
public NumberCellEditor(int numberDigits, int fractionDigits)
{
super(new JFormattedTextField());
System.out.println("Nowa instancja");
ftf = (JFormattedTextField) getComponent();
//Set up the editor for the number cells.
numberFormat = (DecimalFormat) NumberFormat.getInstance();
numberFormat.setMaximumIntegerDigits(numberDigits);
numberFormat.setMaximumFractionDigits(fractionDigits);
NumberFormatter numFormatter = new NumberFormatter(numberFormat);
numFormatter.setFormat(numberFormat);
ftf.setFormatterFactory(new DefaultFormatterFactory(numFormatter));
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
}
//Override to invoke setValue on the formatted text field.
public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
System.out.println("getTableCellEditComp()");
JFormattedTextField ftf =
(JFormattedTextField) super.getTableCellEditorComponent(
table,
value,
isSelected,
row,
column);
if (value != null)
System.out.println(value.toString());
ftf.setValue(value);
return ftf;
}
//Override to ensure that the value remains an Double.
public Object getCellEditorValue()
{
System.out.println("getCellEditorValue()");
JFormattedTextField ftf = (JFormattedTextField) getComponent();
Object o = ftf.getValue();
if (o instanceof Double)
{
System.out.println("Rozpozna³em doubla");
return o;
}
else if (o instanceof Number)
{
System.out.println("Rozpozna³em number");
return new Double(((Number) o).intValue());
}
else
{
System.out.println("Rozpozna³em co¶");
if (DEBUG)
{
System.out.println("getCellEditorValue: o isn't a Number");
}
try
{
return numberFormat.parseObject(o.toString());
}
catch (ParseException exc)
{
System.err.println("getCellEditorValue: can't parse o:
" + o);
return null;
}
}
}
//Override to check whether the edit is valid,
//setting the value if it is and complaining if
//it isn't. If it's OK for the editor to go
//away, we need to invoke the superclass's version
//of this method so that everything gets cleaned up.
public boolean stopCellEditing()
{
System.out.println("stopEditting()");
JFormattedTextField ftf = (JFormattedTextField) getComponent();
if (ftf.isEditValid())
{
try
{
ftf.commitEdit();
}
catch (java.text.ParseException exc)
{
System.out.println("Nie móg³ sparsowaæ.");
}
}
else
{ //text is invalid
((JComponent) getComponent()).setBorder(new
LineBorder(Color.red));
return false; //don't let the editor go away
}
return super.stopCellEditing();
}
}
With class included below I define editor for JTable column.
JFormattedTextFiels behaves in confusing way: when I enter any string
that gives a chance to be interpreted as number formatter says that
everything is OK, and I can tolerate such a behaviour, but then garbage
input is not translated into number but keeps being displayed until I
reeneter the cell, than in almost magical way the value becomes properly
formatted - there are println's in code to show data flow, but there is
sth. behind the scenes what I do not understed.
I would greatly appreciate any help.
public class NumberCellEditor extends DefaultCellEditor
{
JFormattedTextField ftf;
DecimalFormat numberFormat;
private boolean DEBUG = false;
public NumberCellEditor(int numberDigits, int fractionDigits)
{
super(new JFormattedTextField());
System.out.println("Nowa instancja");
ftf = (JFormattedTextField) getComponent();
//Set up the editor for the number cells.
numberFormat = (DecimalFormat) NumberFormat.getInstance();
numberFormat.setMaximumIntegerDigits(numberDigits);
numberFormat.setMaximumFractionDigits(fractionDigits);
NumberFormatter numFormatter = new NumberFormatter(numberFormat);
numFormatter.setFormat(numberFormat);
ftf.setFormatterFactory(new DefaultFormatterFactory(numFormatter));
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
}
//Override to invoke setValue on the formatted text field.
public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
System.out.println("getTableCellEditComp()");
JFormattedTextField ftf =
(JFormattedTextField) super.getTableCellEditorComponent(
table,
value,
isSelected,
row,
column);
if (value != null)
System.out.println(value.toString());
ftf.setValue(value);
return ftf;
}
//Override to ensure that the value remains an Double.
public Object getCellEditorValue()
{
System.out.println("getCellEditorValue()");
JFormattedTextField ftf = (JFormattedTextField) getComponent();
Object o = ftf.getValue();
if (o instanceof Double)
{
System.out.println("Rozpozna³em doubla");
return o;
}
else if (o instanceof Number)
{
System.out.println("Rozpozna³em number");
return new Double(((Number) o).intValue());
}
else
{
System.out.println("Rozpozna³em co¶");
if (DEBUG)
{
System.out.println("getCellEditorValue: o isn't a Number");
}
try
{
return numberFormat.parseObject(o.toString());
}
catch (ParseException exc)
{
System.err.println("getCellEditorValue: can't parse o:
" + o);
return null;
}
}
}
//Override to check whether the edit is valid,
//setting the value if it is and complaining if
//it isn't. If it's OK for the editor to go
//away, we need to invoke the superclass's version
//of this method so that everything gets cleaned up.
public boolean stopCellEditing()
{
System.out.println("stopEditting()");
JFormattedTextField ftf = (JFormattedTextField) getComponent();
if (ftf.isEditValid())
{
try
{
ftf.commitEdit();
}
catch (java.text.ParseException exc)
{
System.out.println("Nie móg³ sparsowaæ.");
}
}
else
{ //text is invalid
((JComponent) getComponent()).setBorder(new
LineBorder(Color.red));
return false; //don't let the editor go away
}
return super.stopCellEditing();
}
}