A table is defined with a custom Cell Editor which uses a JFormattedTextField.
The isEditValid() routine of the JFormattedTextField will be used to determine whether addition processing will take place so its important for it to return the correct value.
The issue is isEditValid() returns true at times and other times it returns false using the same negative value. The only difference is the version of the Java.
import java.awt.*;
import java.math.RoundingMode;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
class SimpleTest extends JFrame
{
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
private String[] columnNames=new String[3];
private String[][] dataValues=new String[3][3];
public SimpleTest()
{
setTitle("JTable Cell Validation");
setSize(300,300);
setDefaultCloseOperation (EXIT_ON_CLOSE);
topPanel=new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
columnNames=new String[] {"Name" ,"Value"};
dataValues=new String[][] { {"DummyField ","0"} };
TableModel model=new myTableModel();
table=new JTable();
table.setModel(model);
table.getColumnModel().getColumn(1).setCellEditor(new MyCustomerEditor());
table.setCellSelectionEnabled(true);
scrollPane=new JScrollPane(table);
topPanel.add(scrollPane,BorderLayout.CENTER);
}
public class myTableModel extends DefaultTableModel
{
myTableModel()
{
super(dataValues,columnNames);
}
public boolean isCellEditable(int row,int cols)
{
if (cols == 0)
return false;
return true;
}
}
public static void main(String args[])
{
SimpleTest x=new SimpleTest();
x.setVisible(true);
}
public class MyCustomerEditor extends DefaultCellEditor
{
private static final long serialVersionUID = 1L;
int attrLength;
public MyCustomerEditor()
{
super(new JFormattedTextField());
System.out.println("Inside the editor.");
final NumberFormat integerFormat = NumberFormat.getIntegerInstance();
integerFormat.setGroupingUsed(false);
integerFormat.setRoundingMode(RoundingMode.DOWN);
integerFormat.setParseIntegerOnly(true);
final NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setOverwriteMode(false);
final JFormattedTextField formattedTextField = (JFormattedTextField) getComponent();
formattedTextField.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
formattedTextField.setHorizontalAlignment(SwingConstants.RIGHT);
formattedTextField.setFocusLostBehavior(JFormattedTextField.PERSIST);
}
@Override
public boolean stopCellEditing()
{
System.out.println("Stopped editing");
if (processData())
{
return false;
}
return super.stopCellEditing();
}
boolean processData()
{
final JFormattedTextField textField = (JFormattedTextField) super.getComponent();
final Object o = super.getCellEditorValue();
final String integer = o.toString();
System.out.println("Processing Integer value : " + integer);
System.out.println(" textField.getText() : " + textField.getText());
System.out.println(" textField.isEditValid() : " + textField.isEditValid());
return true;
}
}
}
When it works, the JTable appears.
Double click the Value field to enter edit mode and enter -1 then press the Enter key.
The output generated.
When it does NOT work, here is the output generated. Oh, also, the Enter key does not work. You will need to click off the field for output to generate.
Returns FALSE with Corretto Version 11
Returns TRUE with Oracle Version 1.8
Does anyone know of any issues with 11.0.10 or have any idea what may be generating the different results?
Edited to add:
The 3 different versions are installed on 3 different machines. Could a locale be a factor in the behavior and not the Java version?
Edited again to add:
This appears to be a locale issue. After passing in locale values via the -D option when executing, I can see the issue work and fail on the same machine simply by changing the value.
The new question is how to address something like this?
Is there another call other than isEditValid() that could be used and that won't be affected by a locale?




You aren’t properly ending a cell edit, because you are not allowing the DefaultCellEditor’s
stopCellEditingmethod to run:JTable needs a proper implementation of stopCellEditing. By preventing that method from doing anything, you are breaking the table.
The solution is to make sure super.stopCellEditing() always runs: