1

In my code I have some JTextFields and also a separate JTable with multiple columns and rows. I want to validate a particular cell in my table, and if it is invalid, don't allow the user to move the cursor to another field. My code works correctly if the user moves the cursor to a different cell in the table (different row or column), but if I move the cursor to a different field on the form, the cursor is moved. I have verified that my stopCellEditing function is called and returning false. I turn the border red when it is invalid. That is working correctly and as expected, just the cursor is moving. Here is my code.

The cell editor to use for the column affected is set using

    // Set up the verifier to make sure the user has entered a valid value
    statusTable.getColumn(statusTable.getColumnName(1)).
            setCellEditor(new CellEditor(new SvidVerifier(), this));

And my extended DefaultCellEditor is

class CellEditor extends DefaultCellEditor {

  InputVerifier verifier = null;
  ModView view = null;

  public CellEditor(InputVerifier verifier, ModView view) {
    super(new JTextField());
    this.verifier = verifier;
    this.view = view;
  }

  @Override
  public boolean stopCellEditing() {
    if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
            getFocusOwner() == view.publicBrowseButton) {
      super.cancelCellEditing();
      return true;
    }
    boolean canStop = verifier.verify(editorComponent) &&
            super.stopCellEditing();
    return canStop;
  }
}

and my verifier is

class IdVerifier extends InputVerifier {
  @Override
  public boolean verify(JComponent input) {
    JTextField tf = (JTextField) input;
    try {
      if ((Integer.parseInt(tf.getText()) >= 1) &&
              (Integer.parseInt(tf.getText()) <= 32)) {
        tf.setBorder(new LineBorder(
                UIManager.getColor("activeCaptionBorder")));
        return true;
      } else {
        tf.setBorder(new LineBorder(Color.RED));
        return false;
      }
    } catch (Exception ex) {
      tf.setBorder(new LineBorder(Color.RED));
      return false;
    }
  }
}

Thank you for your help.

Louis
  • 11
  • 1
  • Check out: https://stackoverflow.com/questions/20390445/swing-catching-exceptions-from-tablemodel/20390486#20390486 for an example editor that forces you to enter a string of "5 characters. You should be able to customize the working example to meet your editing requirement. – camickr Jan 14 '19 at 02:04
  • @camickr This was the exact thing that I needed to do. Simple and effective. Thank you. – Louis Jan 14 '19 at 18:48

2 Answers2

0

As I see it, the table is told to stop editing but nothing more. The CellEditor interface does not mention focus handling for stopCellEditing.

You may have to bind a focus listener to the other components on the form and in that listener, verify whether previous focus was on the table. If so and the table is still editing, set focus back to the table.

TT.
  • 15,774
  • 6
  • 47
  • 88
  • I was suspecting that I might need to do something like this, but the bookkeeping aspect of it wasn't what I wanted, especially since I (erroneously) felt that what I was doing should have taken care of the issue. I used @camickr response (pointing to a different page) and it worked perfectly. Thank you for your response. – Louis Jan 14 '19 at 18:50
0

Using the article that @camickr pointed out I was able to successfully accomplish what I needed and solve my problem by using the TextField.requestFocusInWindow() function.

Louis
  • 11
  • 1