DefaultCellEditor and its component's JFormattedTextField properties are not applied on the first click, but only at the second one

22 views Asked by At

I have a code that runs the DefaultCellEditor on the JTable's cell single click. I expect, that at the same first click the cell will have null border, MaskFormatter will format the field and field will have some value set to it and having caret at the beginning (leftmost).

  JFormattedTextField priceField = new JFormattedTextField();
    try {
      priceField.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("#.####")));
      priceField.setValue("0.0000");
      priceField.setBorder(null);
      priceField.setCaretPosition(0);

    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
    TableColumn priceColumn = changeTrafficProviderTable.getColumnModel().getColumn(4);
    DefaultCellEditor priceCellEditor = new DefaultCellEditor(priceField);
    priceCellEditor.setClickCountToStart(0);
    priceColumn.setCellEditor(priceCellEditor);

    JFormattedTextField prefixField = new JFormattedTextField();
    try {
      prefixField.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
      prefixField.setBorder(null);
      prefixField.setValue("00");
      prefixField.setCaretPosition(0);
      
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
    TableColumn prefixColumn = changeTrafficProviderTable.getColumnModel().getColumn(3);
    DefaultCellEditor prefixCellEditor = new DefaultCellEditor(prefixField);
    prefixCellEditor.setClickCountToStart(0);
    prefixColumn.setCellEditor(prefixCellEditor);

In fact, I have only null border and MaskFormatter applied at the first click. The value set and caret put to the beginning (left) is done on the second click.

I can't find any event associated with the first click and the second click to debug the different behavior. Please give some ideas how to fix that.

  1. The first click, I have caret blinking. It is at the end and the text isn't set

enter image description here

enter image description here

  1. Do some click ountsine on another field:

enter image description here

  1. Click to Price column again. The text is set, and the caret is placed according to the code

enter image description here

So, why the neighbor lines of code are applied at two moments?

0

There are 0 answers