Results 1 to 5 of 5

Thread: Allowing a empty/null selection in a bound ComboBox

  1. #1

    Default Allowing a empty/null selection in a bound ComboBox

    I currently bound some proerties of an object (which are objects themselves) to a JComboBox, using a ValueHolder. Some properties allow null values in the database and now I'm stuck on trying to insert a representation of such a "null value" into the ValueModel/ComboBox. 'null' itself is not allowed and I can't think of any other representation for it.

    Code:
    List<User> users = userManager.getUsers();
    //users.add(0, null);    // does not work, what else might be possible?
    ValueHolder vhUsers = new ValueHolder(users);
    tfb.add(sbf.createBoundComboBox("userAccount", vhUsers, "username"));
    I searched the whole forum but couln't find a hint on how to do it (or just don't see the wood for the trees...)

  2. #2
    Join Date
    Mar 2007
    Location
    Germany, Tuebingen
    Posts
    49

    Default

    Hi,

    I think the only solution will be to insert an "empty" user object instead
    of a null. Otherwise I think you will alwas get an exception when
    Spring RCP tries to access the userAccount and username properties of
    the user object.
    Maybe the easiest way is to check if one of the entries in the list is null and
    add there an empty user object.

    Regards,
    Torsten

  3. #3

    Default

    The problem with this approach is, that Hibernate will try to save this new user via a cascade. But you're right, maybe I'll hack some custom-built handling for empty user objects just before the commit which sets the property back to null, might be the easiest solution. Just thought that there might be a clever solution for this use case in Spring-RCP.

  4. #4
    Join Date
    Feb 2013
    Posts
    3

    Default

    I found that, the ComboBox interceptor (org.springframework.richclient.list.ComboBoxAutoC ompletionInterceptorFactory.ComboBoxAutoCompletion Interceptor) makes a not editable combobox editable, and allows the null option, but, if you select any item in the combo, then it doesn't allow to unselect it.
    I change the combo editor, to allow with the "delete" key, remove any selected item,
    at the class ComboBoxAutoCompletion.ChangeHandler at the method 'keyPressed':
    public void keyPressed(KeyEvent e) {
    hitBackspace = false;
    switch (e.getKeyCode()) {
    case KeyEvent.VK_ENTER:
    highlightCompletedText(0);
    break;
    // determine if the pressed key is backspace (needed by the remove
    // method)
    case KeyEvent.VK_BACK_SPACE:
    hitBackspace = true;
    hitBackspaceOnSelection = editor.getSelectionStart() != editor.getSelectionEnd();
    break;

    // ignore delete key
    case KeyEvent.VK_DELETE:
    // abentan: commented to allow the remove feature
    // e.consume();
    // ComboBoxAutoCompletion.this.comboBox.getToolkit(). beep();
    // abentan: added to allow the remove feature
    model.setSelectedItem(null);
    break;
    }
    }

    I'm still testing, but I haven't found any problems.

  5. #5
    Join Date
    Feb 2013
    Posts
    3

    Default

    With this:
    - remove the ctrl+x bug: if one select an item at the combo, and then press ctrl+x, no item is selected, but the model is not changed. Now it will put a null object in the model
    - backspace: if all the text is selected, the backspace key place a null object in the model
    - delete: the delete key will put a null object in the model if all the text is selected

    // Highlight whole text when user hits enter
    // Register when user hits backspace
    public void keyPressed(KeyEvent e) {
    hitBackspace = false;
    switch (e.getKeyCode()) {
    case KeyEvent.VK_ENTER:
    highlightCompletedText(0);
    break;
    // determine if the pressed key is backspace (needed by the remove
    // method)
    case KeyEvent.VK_BACK_SPACE:
    hitBackspace = true;
    hitBackspaceOnSelection = editor.getSelectionStart() != editor.getSelectionEnd();

    // agregado por abentan para permitir seleccionar vacío
    if (editor.getSelectionStart() == 0 && editor.getSelectionEnd() == getLength())
    {
    try
    {
    setText(null);
    }
    catch (BadLocationException ex)
    {
    Logger.getLogger(ComboBoxAutoCompletion.class.getN ame()).log(Level.SEVERE, null, ex);
    }
    model.setSelectedItem(null);
    }
    // fin agregado por abentan
    break;

    // agregado por abentan para corregir el ctrl+x
    case KeyEvent.VK_X:
    if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)
    {
    model.setSelectedItem(null);
    }
    break;
    // fin agregado por abentan
    //modificado por abentan, solo comentado para permitir seleccionar vacío en un combobox no editable
    // ignore delete key
    case KeyEvent.VK_DELETE:
    // comentado por abentan
    // e.consume();
    // ComboBoxAutoCompletion.this.comboBox.getToolkit(). beep();
    // agregado por abentan
    if (editor.getSelectionStart() == 0 && editor.getSelectionEnd() == getLength())
    {
    model.setSelectedItem(null);
    } else
    {
    e.consume();
    ComboBoxAutoCompletion.this.comboBox.getToolkit(). beep();
    }
    // fin agregado por abentan
    break;
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •