AutoCompletar Combobox mais flexivel

opa, consegui implementar o ClasseAutoComplete sugerido pelo ViniGodoy desta forma:

 [code]

// formata a combo
rivate void decoraComboContrib(){
grafica.ClasseAutoComplete.decorate(ComboContrib);
}

//preenche a combo
private void ComboContrib(){
Object codSituacao,nomeSituacoes;
conn.executeSQL(“SELECT s.id_situacao,s.nome_situacoes”
+" FROM SITUACOES s ORDER BY id_situacao");
try {
ComboContrib.addItem(“Selecione”);
while (conn.resultset.next()) {
codSituacao = conn.resultset.getInt(“id_situacao”);
nomeSituacoes = conn.resultset.getString(“nome_situacoes”);
ComboContrib.addItem(nomeSituacoes);
}
} catch (SQLException ex) {
Logger.getLogger(NovoServico.class.getName()).log(Level.SEVERE, null, ex);
}
} [/code]

até ai blz, só gostaria de saber se tem como liberar a digitação de dados na combo q nao estejam na mesma combo.

ex.:
tipo tenho uma lista ja cadastrada na combo

“Carlos” e “José”

entao agora se você digitar “M” nada acontece, pq está “bloqueado”.

gostaria de saber se tem como liberar para que eu possa digitar outros dados mesmo que nao tenha no cadastro???

valeu

Os componentes SwingX oferecem a funcionalidade que você precisa:
http://www.swinglabs.org/downloads.jsp

Os links que aparece no link que vc me passou estão dando um erro no site…

The file /web/projects/swingx/files/documents/2981/153006/swingx-1.6.2-sources.zip appears to be missing.

valeu

achei o mesmo arquivo no 4shared.

fiz o download só nao sei como faço pra instalar no netbeans?

valeu

pessoal descobri aki pesquinsando é que preciso que o autocomplete deve ser nonstrict para que possa ser digitado outros dados além do que está cadastrado.

como faço pra liberar isso no autoComplete abaixo?

[code]
package model;

import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.ComboBoxEditor;
import javax.swing.InputMap;
import javax.swing.JComboBox;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;
import javax.swing.text.TextAction;

import org.jdesktop.swingx.autocomplete.AbstractAutoCompleteAdaptor;
import org.jdesktop.swingx.autocomplete.AutoCompleteComboBoxEditor;
import org.jdesktop.swingx.autocomplete.AutoCompleteDocument;
import org.jdesktop.swingx.autocomplete.ComboBoxAdaptor;
import org.jdesktop.swingx.autocomplete.ObjectToStringConverter;
import org.jdesktop.swingx.autocomplete.workarounds.AquaLnFPopupLocationFix;

/**

  • This class contains only static utility methods that can be used to set up

  • automatic completion for some Swing components.

  • Usage examples:

  • 
    
  • JComboBox comboBox = […];

  • AutoCompleteDecorator.decorate(comboBox);

  • List items = […];

  • JTextField textField = […];

  • AutoCompleteDecorator.decorate(textField, items);

  • JList list = […];

  • JTextField textField = […];

  • AutoCompleteDecorator.decorate(list, textField);

  • @author Thomas Bierhance
    */
    public class ClasseAutoComplete{

    /**

    • Enables automatic completion for the given JComboBox. The automatic
    • completion will be strict (only items from the combo box can be selected)
    • if the combo box is not editable.
    • @param comboBox a combo box
      */
      public static void decorate(final JComboBox comboBox){
      decorate(comboBox, ObjectToStringConverter.DEFAULT_IMPLEMENTATION);
      }

    /**

    • Enables automatic completion for the given JComboBox. The automatic

    • completion will be strict (only items from the combo box can be selected)

    • if the combo box is not editable.

    • @param comboBox a combo box

    • @param stringConverter the converter used to transform items to strings
      */
      public static void decorate(final JComboBox comboBox, final ObjectToStringConverter stringConverter){
      boolean strictMatching = !comboBox.isEditable();
      // has to be editable
      comboBox.setEditable(true);
      // fix the popup location
      AquaLnFPopupLocationFix.install(comboBox);

      // configure the text component=editor component
      JTextComponent editorComponent = (JTextComponent) comboBox.getEditor().getEditorComponent();
      final AbstractAutoCompleteAdaptor adaptor = new ComboBoxAdaptor(
      comboBox);
      final AutoCompleteDocument document = new AutoCompleteDocument(adaptor,
      strictMatching, stringConverter);
      decorate(editorComponent, document, adaptor);

      // show the popup list when the user presses a key
      final KeyListener keyListener = new KeyAdapter(){
      @Override
      public void keyPressed(KeyEvent keyEvent){
      // don’t popup on action keys (cursor movements, etc…)
      if (keyEvent.isActionKey())
      return;
      // don’t popup if the combobox isn’t visible anyway
      if (comboBox.isDisplayable() && !comboBox.isPopupVisible()){
      int keyCode = keyEvent.getKeyCode();
      // don’t popup when the user hits shift,ctrl or alt
      if (keyCode == KeyEvent.VK_SHIFT
      || keyCode == KeyEvent.VK_CONTROL
      || keyCode == KeyEvent.VK_ALT)
      return;
      // don’t popup when the user hits escape (see issue #311)
      if (keyCode == KeyEvent.VK_ESCAPE
      || keyCode == KeyEvent.VK_ENTER)
      return;

               if (keyEvent.isAltDown() || keyEvent.isControlDown())
                   return;
      
               comboBox.setPopupVisible(true);
           }
       }
      

      };
      editorComponent.addKeyListener(keyListener);

      if (stringConverter != ObjectToStringConverter.DEFAULT_IMPLEMENTATION){
      comboBox.setEditor(new AutoCompleteComboBoxEditor(
      comboBox.getEditor(), stringConverter));
      }

      // Changing the l&f can change the combobox’ editor which in turn
      // would not be autocompletion-enabled. The new editor needs to be
      // set-up.
      comboBox.addPropertyChangeListener(“editor”,
      new PropertyChangeListener(){
      public void propertyChange(PropertyChangeEvent e){
      ComboBoxEditor editor = (ComboBoxEditor) e.getNewValue();
      if (editor != null
      && editor.getEditorComponent() != null){
      if (!(editor instanceof AutoCompleteComboBoxEditor)
      && stringConverter != ObjectToStringConverter.DEFAULT_IMPLEMENTATION){
      comboBox.setEditor(new AutoCompleteComboBoxEditor(
      editor, stringConverter));
      // Don’t do the decorate step here because
      // calling setEditor will trigger
      // the propertychange listener a second time,
      // which will do the decorate
      // and addKeyListener step.
      }
      else{
      decorate(
      (JTextComponent) editor.getEditorComponent(),
      document, adaptor);
      editor.getEditorComponent().addKeyListener(
      keyListener);
      }
      }
      }
      });
      }

    /**

    • Decorates a given text component for automatic completion using the given

    • AutoCompleteDocument and AbstractAutoCompleteAdaptor.

    • @param textComponent a text component that should be decorated

    • @param document the AutoCompleteDocument to be installed on the text

    •    component
      
    • @param adaptor the AbstractAutoCompleteAdaptor to be used
      */
      public static void decorate(JTextComponent textComponent,
      AutoCompleteDocument document,
      final AbstractAutoCompleteAdaptor adaptor){
      // install the document on the text component
      textComponent.setDocument(document);

      // mark entire text when the text component gains focus
      // otherwise the last mark would have been retained which is quiet
      // confusing
      textComponent.addFocusListener(new FocusAdapter(){
      @Override
      public void focusGained(FocusEvent e)
      {
      adaptor.markEntireText();
      }
      });

      // Tweak some key bindings
      InputMap editorInputMap = textComponent.getInputMap();
      if (document.isStrictMatching()){
      // move the selection to the left on VK_BACK_SPACE
      editorInputMap.put(KeyStroke.getKeyStroke(
      java.awt.event.KeyEvent.VK_BACK_SPACE, 0),
      DefaultEditorKit.selectionBackwardAction);
      // ignore VK_DELETE and CTRL+VK_X and beep instead when strict
      // matching
      editorInputMap.put(KeyStroke.getKeyStroke(
      java.awt.event.KeyEvent.VK_DELETE, 0), errorFeedbackAction);
      editorInputMap.put(KeyStroke.getKeyStroke(
      java.awt.event.KeyEvent.VK_X,
      java.awt.event.InputEvent.CTRL_DOWN_MASK),
      errorFeedbackAction);
      }else {
      ActionMap editorActionMap = textComponent.getActionMap();
      // leave VK_DELETE and CTRL+VK_X as is
      // VK_BACKSPACE will move the selection to the left if the selected
      // item is in the list
      // it will delete the previous character otherwise
      editorInputMap.put(KeyStroke.getKeyStroke(
      java.awt.event.KeyEvent.VK_BACK_SPACE, 0),
      “nonstrict-backspace”);
      editorActionMap.put(
      “nonstrict-backspace”,
      new NonStrictBackspaceAction(
      editorActionMap.get(DefaultEditorKit.deletePrevCharAction),
      editorActionMap.get(DefaultEditorKit.selectionBackwardAction),
      adaptor));
      }
      }

    static class NonStrictBackspaceAction extends TextAction{
    Action backspace;
    Action selectionBackward;
    AbstractAutoCompleteAdaptor adaptor;

     public NonStrictBackspaceAction(Action backspace,
             Action selectionBackward, AbstractAutoCompleteAdaptor adaptor){
         super("nonstrict-backspace");
         this.backspace = backspace;
         this.selectionBackward = selectionBackward;
         this.adaptor = adaptor;
     }
    
     public void actionPerformed(ActionEvent e){
         if (adaptor.listContainsSelectedItem()){
             selectionBackward.actionPerformed(e);
         }else{
             backspace.actionPerformed(e);
         }
     }
    

    }

    /**

    • A TextAction that provides an error feedback for the text component that
    • invoked the action. The error feedback is most likely a “beep”.
      */
      static Object errorFeedbackAction = new TextAction(“provide-error-feedback”){
      public void actionPerformed(ActionEvent e){
      UIManager.getLookAndFeel().provideErrorFeedback(getTextComponent(e));
      }
      };
      }[/code]

dai pessoal…

consegui fazer o que eu qria…

para quem estava na mesma dúvida que eu ta ai o código pronto.

[code]
package model;

import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.ComboBoxEditor;
import javax.swing.InputMap;
import javax.swing.JComboBox;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;
import javax.swing.text.TextAction;

import org.jdesktop.swingx.autocomplete.AbstractAutoCompleteAdaptor;
import org.jdesktop.swingx.autocomplete.AutoCompleteComboBoxEditor;
import org.jdesktop.swingx.autocomplete.AutoCompleteDocument;
import org.jdesktop.swingx.autocomplete.ComboBoxAdaptor;
import org.jdesktop.swingx.autocomplete.ObjectToStringConverter;
import org.jdesktop.swingx.autocomplete.workarounds.AquaLnFPopupLocationFix;

/**

  • This class contains only static utility methods that can be used to set up

  • automatic completion for some Swing components.

  • Usage examples:

  •   
    
  • JComboBox comboBox = […];

  • AutoCompleteDecorator.decorate(comboBox);

  • List items = […];

  • JTextField textField = […];

  • AutoCompleteDecorator.decorate(textField, items);

  • JList list = […];

  • JTextField textField = […];

  • AutoCompleteDecorator.decorate(list, textField);

  • @author Thomas Bierhance
    */
    public class ClasseAutoComplete{

    /**

    • Enables automatic completion for the given JComboBox. The automatic
    • completion will be strict (only items from the combo box can be selected)
    • if the combo box is not editable.
    • @param comboBox a combo box
      */
      public static void decorate(final JComboBox comboBox){
      decorate(comboBox, ObjectToStringConverter.DEFAULT_IMPLEMENTATION);
      }

    /**

    • Enables automatic completion for the given JComboBox. The automatic

    • completion will be strict (only items from the combo box can be selected)

    • if the combo box is not editable.

    • @param comboBox a combo box

    • @param stringConverter the converter used to transform items to strings
      */
      public static void decorate(final JComboBox comboBox, final ObjectToStringConverter stringConverter){
      boolean strictMatching = !comboBox.isEditable();
      // has to be editable
      comboBox.setEditable(true);
      // fix the popup location
      AquaLnFPopupLocationFix.install(comboBox);

      // configure the text component=editor component
      JTextComponent editorComponent = (JTextComponent) comboBox.getEditor().getEditorComponent();
      final AbstractAutoCompleteAdaptor adaptor = new ComboBoxAdaptor(
      comboBox);
      final AutoCompleteDocument document = new AutoCompleteDocument(adaptor, false); //AKI TA A MUDANÇA - COLOCA O “false” no lugar dos “strictMatching, stringConverter” que estavam antes. valeu!!!
      decorate(editorComponent, document, adaptor);

      // show the popup list when the user presses a key
      final KeyListener keyListener = new KeyAdapter(){
      @Override
      public void keyPressed(KeyEvent keyEvent){
      // don’t popup on action keys (cursor movements, etc…)
      if (keyEvent.isActionKey())
      return;
      // don’t popup if the combobox isn’t visible anyway
      if (comboBox.isDisplayable() && !comboBox.isPopupVisible()){
      int keyCode = keyEvent.getKeyCode();
      // don’t popup when the user hits shift,ctrl or alt
      if (keyCode == KeyEvent.VK_SHIFT
      || keyCode == KeyEvent.VK_CONTROL
      || keyCode == KeyEvent.VK_ALT)
      return;
      // don’t popup when the user hits escape (see issue #311)
      if (keyCode == KeyEvent.VK_ESCAPE
      || keyCode == KeyEvent.VK_ENTER)
      return;

               if (keyEvent.isAltDown() || keyEvent.isControlDown())   
                   return;   
      
               comboBox.setPopupVisible(true);   
           }   
       }   
      

      };
      editorComponent.addKeyListener(keyListener);

      if (stringConverter != ObjectToStringConverter.DEFAULT_IMPLEMENTATION){
      comboBox.setEditor(new AutoCompleteComboBoxEditor(
      comboBox.getEditor(), stringConverter));
      }

      // Changing the l&f can change the combobox’ editor which in turn
      // would not be autocompletion-enabled. The new editor needs to be
      // set-up.
      comboBox.addPropertyChangeListener(“editor”,
      new PropertyChangeListener(){
      public void propertyChange(PropertyChangeEvent e){
      ComboBoxEditor editor = (ComboBoxEditor) e.getNewValue();
      if (editor != null
      && editor.getEditorComponent() != null){
      if (!(editor instanceof AutoCompleteComboBoxEditor)
      && stringConverter != ObjectToStringConverter.DEFAULT_IMPLEMENTATION){
      comboBox.setEditor(new AutoCompleteComboBoxEditor(
      editor, stringConverter));
      // Don’t do the decorate step here because
      // calling setEditor will trigger
      // the propertychange listener a second time,
      // which will do the decorate
      // and addKeyListener step.
      }
      else{
      decorate(
      (JTextComponent) editor.getEditorComponent(),
      document, adaptor);
      editor.getEditorComponent().addKeyListener(
      keyListener);
      }
      }
      }
      });
      }

    /**

    • Decorates a given text component for automatic completion using the given

    • AutoCompleteDocument and AbstractAutoCompleteAdaptor.

    • @param textComponent a text component that should be decorated

    • @param document the AutoCompleteDocument to be installed on the text

    •    component  
      
    • @param adaptor the AbstractAutoCompleteAdaptor to be used
      */
      public static void decorate(JTextComponent textComponent,
      AutoCompleteDocument document,
      final AbstractAutoCompleteAdaptor adaptor){
      // install the document on the text component
      textComponent.setDocument(document);

      // mark entire text when the text component gains focus
      // otherwise the last mark would have been retained which is quiet
      // confusing
      textComponent.addFocusListener(new FocusAdapter(){
      @Override
      public void focusGained(FocusEvent e)
      {
      adaptor.markEntireText();
      }
      });

      // Tweak some key bindings
      InputMap editorInputMap = textComponent.getInputMap();
      if (document.isStrictMatching()){
      // move the selection to the left on VK_BACK_SPACE
      editorInputMap.put(KeyStroke.getKeyStroke(
      java.awt.event.KeyEvent.VK_BACK_SPACE, 0),
      DefaultEditorKit.selectionBackwardAction);
      // ignore VK_DELETE and CTRL+VK_X and beep instead when strict
      // matching
      editorInputMap.put(KeyStroke.getKeyStroke(
      java.awt.event.KeyEvent.VK_DELETE, 0), errorFeedbackAction);
      editorInputMap.put(KeyStroke.getKeyStroke(
      java.awt.event.KeyEvent.VK_X,
      java.awt.event.InputEvent.CTRL_DOWN_MASK),
      errorFeedbackAction);
      }else {
      ActionMap editorActionMap = textComponent.getActionMap();
      // leave VK_DELETE and CTRL+VK_X as is
      // VK_BACKSPACE will move the selection to the left if the selected
      // item is in the list
      // it will delete the previous character otherwise
      editorInputMap.put(KeyStroke.getKeyStroke(
      java.awt.event.KeyEvent.VK_BACK_SPACE, 0),
      “nonstrict-backspace”);
      editorActionMap.put(
      “nonstrict-backspace”,
      new NonStrictBackspaceAction(
      editorActionMap.get(DefaultEditorKit.deletePrevCharAction),
      editorActionMap.get(DefaultEditorKit.selectionBackwardAction),
      adaptor));
      }
      }

    static class NonStrictBackspaceAction extends TextAction{
    Action backspace;
    Action selectionBackward;
    AbstractAutoCompleteAdaptor adaptor;

     public NonStrictBackspaceAction(Action backspace,   
             Action selectionBackward, AbstractAutoCompleteAdaptor adaptor){   
         super("nonstrict-backspace");   
         this.backspace = backspace;   
         this.selectionBackward = selectionBackward;   
         this.adaptor = adaptor;   
     }   
    
     public void actionPerformed(ActionEvent e){   
         if (adaptor.listContainsSelectedItem()){   
             selectionBackward.actionPerformed(e);   
         }else{   
             backspace.actionPerformed(e);   
         }   
     }   
    

    }

    /**

    • A TextAction that provides an error feedback for the text component that
    • invoked the action. The error feedback is most likely a “beep”.
      */
      static Object errorFeedbackAction = new TextAction(“provide-error-feedback”){
      public void actionPerformed(ActionEvent e){
      UIManager.getLookAndFeel().provideErrorFeedback(getTextComponent(e));
      }
      };
      } [/code]