Laf-Widget client properties

View all client properties.


Client property name

LafWidget.COMBO_BOX_AUTOCOMPLETION_MATCHER

Description

Client property name for specifying the auto-completion matcher on editable comboboxes. This property can be set either on a single combobox or globally on UIManager. The value in both cases should be an instance of org.jvnet.lafwidget.combo.AutoCompletionMatcher. The default implementation is in org.jvnet.lafwidget.combo.DefaultAutoCompletionMatcher and uses case-ignore string matching.


See also


Sample code

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.jvnet.lafwidget.LafWidget;
import org.jvnet.lafwidget.combo.AutoCompletionMatcher;
import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;

/**
 * Test application that shows the use of the
 {@link LafWidget#COMBO_BOX_USE_MODEL_ONLY} client property.
 
 @author Kirill Grouchnikov
 @see LafWidget#COMBO_BOX_USE_MODEL_ONLY
 */
public class ComboBoxAutoCompletionMatcher extends JFrame {
  /**
   * Case sensitive matcher for combobox auto-completion.
   
   @author Kirill Grouchnikov
   */
  protected static class CaseSensitiveMatcher implements
      AutoCompletionMatcher {
    public Object getFirstMatching(ComboBoxModel model, String pattern) {
      Object selectedItem = model.getSelectedItem();
      // only search for a different item if the currently
      // selected does not match
      if ((selectedItem != null)
          && selectedItem.toString().startsWith(pattern)) {
        return selectedItem;
      else {
        // iterate over all items
        for (int i = 0, n = model.getSize(); i < n; i++) {
          Object currentItem = model.getElementAt(i);
          // current item starts with the pattern?
          if ((currentItem != null)
              && currentItem.toString().startsWith(pattern)) {
            return currentItem;
          }
        }
      }
      // no item starts with the pattern => return null
      return null;
    }
  }

  /**
   * Creates the main frame for <code>this</code> sample.
   */
  public ComboBoxAutoCompletionMatcher() {
    super("Combobox auto-completion custom matcher");

    this.setLayout(new BorderLayout());

    final JComboBox cb = new JComboBox(new Object[] { "Ester""Jordi",
        "Jordina""Jorge""sergi" });
    cb.setEditable(true);

    JPanel main = new JPanel(new FlowLayout(FlowLayout.CENTER));
    this.add(main, BorderLayout.CENTER);
    main.add(cb);

    JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));

    final JCheckBox caseSensitiveAutoCompletion = new JCheckBox(
        "Use case-sensitive auto-completion");
    caseSensitiveAutoCompletion.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cb
            .putClientProperty(
                LafWidget.COMBO_BOX_AUTOCOMPLETION_MATCHER,
                caseSensitiveAutoCompletion.isSelected() new CaseSensitiveMatcher()
                    null);
      }
    });

    controls.add(caseSensitiveAutoCompletion);
    this.add(controls, BorderLayout.SOUTH);

    this.setSize(400200);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  /**
   * The main method for <code>this</code> sample. The arguments are
   * ignored.
   
   @param args
   *            Ignored.
   @throws Exception
   *             If some exception occured. Note that there is no special
   *             treatment of exception conditions in <code>this</code>
   *             sample code.
   */
  public static void main(String[] argsthrows Exception {
    UIManager.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
    JFrame.setDefaultLookAndFeelDecorated(true);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        new ComboBoxAutoCompletionMatcher().setVisible(true);
      }
    });
  }
}

The screenshot below shows an editable combo when this property is not installed. On typing "S", the "serge" entry is selected (the match is not case-sensitive):

After this property has been installed to use a custom matcher that uses case-sensitive match, pressing "S" doesn't select the "serge" entry. Instead, you can type any entry you want (unless the COMBO_BOX_USE_MODEL_ONLY client property is installed):