Auto combo box

ae tenho o seguinte código(2 classae):

//CODE
import java.util.;
import javax.swing.JTextField;
import javax.swing.text.
;

public class Java2sAutoTextField extends JTextField {
class AutoDocument extends PlainDocument {

    public void replace(int i, int j, String s, AttributeSet attributeset)
    throws BadLocationException {
        super.remove(i, j);
        insertString(i, s, attributeset);
    }
    
    public void insertString(int i, String s, AttributeSet attributeset)
    throws BadLocationException {
        if (s == null || "".equals(s))
            return;
        String s1 = getText(0, i);
        String s2 = getMatch(s1 + s);
        int j = (i + s.length()) - 1;
        if (isStrict && s2 == null) {
            s2 = getMatch(s1);
            j--;
        } else if (!isStrict && s2 == null) {
            super.insertString(i, s, attributeset);
            return;
        }
        if (autoComboBox != null && s2 != null)
            autoComboBox.setSelectedValue(s2);
        super.remove(0, getLength());
        super.insertString(0, s2, attributeset);
        setSelectionStart(j + 1);
        setSelectionEnd(getLength());
    }
    
    public void remove(int i, int j) throws BadLocationException {
        int k = getSelectionStart();
        if (k > 0)
            k--;
        String s = getMatch(getText(0, k));
        if (!isStrict && s == null) {
            super.remove(i, j);
        } else {
            super.remove(0, getLength());
            super.insertString(0, s, null);
        }
        if (autoComboBox != null && s != null)
            autoComboBox.setSelectedValue(s);
        try {
            setSelectionStart(k);
            setSelectionEnd(getLength());
        } catch (Exception exception) {
        }
    }
    
}

public Java2sAutoTextField(Vector vector) {
isCaseSensitive = false;
isStrict = true;
autoComboBox = null;
if (vector == null) {
throw new IllegalArgumentException(“values can not be null”);
} else {
dataVector = vector;
init();
return;
}
}

public Java2sAutoTextField(Vector vector, Java2sAutoComboBox b) {
isCaseSensitive = false;
isStrict = true;
autoComboBox = null;
if (vector == null) {
throw new IllegalArgumentException(“values can not be null”);
} else {
dataVector = vector;
autoComboBox = b;
init();
return;
}
}

private void init() {
    setDocument(new AutoDocument());
    if (isStrict && dataVector.size() > 0)
        setText(dataVector.get(0).toString());
}

private String getMatch(String s) {
    for (int i = 0; i < dataVector.size(); i++) {
        String s1 = dataVector.get(i).toString();
        if (s1 != null) {
            if (!isCaseSensitive
                    && s1.toLowerCase().startsWith(s.toLowerCase()))
                return s1;
            if (isCaseSensitive && s1.startsWith(s))
                return s1;
        }
    }
    
    return null;
}

public void Selection(String s) {
    AutoDocument _lb = (AutoDocument) getDocument();
    if (_lb != null)
        try {
            int i = Math.min(getCaret().getDot(), getCaret().getMark());
            int j = Math.max(getCaret().getDot(), getCaret().getMark());
            _lb.replace(i, j - i, s, null);
        } catch (Exception exception) {
        }
}

public boolean isCaseSensitive() {
    return isCaseSensitive;
}

public void setCaseSensitive(boolean flag) {
    isCaseSensitive = flag;
}

public boolean isStrict() {
    return isStrict;
}

public void setStrict(boolean flag) {
    isStrict = flag;
}

public Vector getDataVector() {
    return dataVector;
}

public void setDataVector(Vector vector) {
    if (vector == null) {
        throw new IllegalArgumentException("values can not be null");
    } else {
        dataVector = vector;
        return;
    }
}

private Vector dataVector;

private boolean isCaseSensitive;

private boolean isStrict;

private Java2sAutoComboBox autoComboBox;

}
//CODE

//CODE
import java.awt.event.ItemEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.plaf.basic.BasicComboBoxEditor;

public class Java2sAutoComboBox extends JComboBox {
private class AutoTextFieldEditor extends BasicComboBoxEditor {

    private Java2sAutoTextField getAutoTextFieldEditor() {
        return (Java2sAutoTextField) editor;
    }
    
    AutoTextFieldEditor(java.util.Vector vector) {
        editor = new Java2sAutoTextField(vector, Java2sAutoComboBox.this);
    }
}

public Java2sAutoComboBox(java.util.Vector vector) {
    isFired = false;
    autoTextFieldEditor = new AutoTextFieldEditor(vector);
    setEditable(true);
    setModel(new DefaultComboBoxModel(vector.toArray()) {
        
        protected void fireContentsChanged(Object obj, int i, int j) {
            if (!isFired)
                super.fireContentsChanged(obj, i, j);
        }
        
    });
    setEditor(autoTextFieldEditor);
}

public boolean isCaseSensitive() {
    return autoTextFieldEditor.getAutoTextFieldEditor().isCaseSensitive();
}

public void setCaseSensitive(boolean flag) {
    autoTextFieldEditor.getAutoTextFieldEditor().setCaseSensitive(flag);
}

public boolean isStrict() {
    return autoTextFieldEditor.getAutoTextFieldEditor().isStrict();
}

public void setStrict(boolean flag) {
    autoTextFieldEditor.getAutoTextFieldEditor().setStrict(flag);
}

public java.util.Vector getDataVector() {
    return autoTextFieldEditor.getAutoTextFieldEditor().getDataVector();
}

public void setDataVector(java.util.Vector vector) {
    autoTextFieldEditor.getAutoTextFieldEditor().setDataVector(vector);
    setModel(new DefaultComboBoxModel(vector.toArray()));
}

void setSelectedValue(Object obj) {
    if (isFired) {
        return;
    } else {
        isFired = true;
        setSelectedItem(obj);
        fireItemStateChanged(new ItemEvent(this, 701, selectedItemReminder, 1));
        isFired = false;
        return;
    }
}

protected void fireActionEvent() {
    if (!isFired)
        super.fireActionEvent();
}

private AutoTextFieldEditor autoTextFieldEditor;

private boolean isFired;

}
//CODE

eu testei da seguinte forma:
//CODE
package testeautocomplete;

import java.awt.event.;
import java.util.
;
import javax.swing.;
import java.awt.
;

public class Teste extends JFrame {
private Vector v;
private String opcoes[] = {“gato”, “cachorro”, “mamute”, “viado”, “cavalo”};
/** Creates a new instance of Teste */
public Teste() {
v = new Vector();
for (int i =0; i < opcoes.length; i++){
v.add(opcoes[i]);
}
Java2sAutoComboBox jac = new Java2sAutoComboBox(v);
Java2sAutoTextField jtf = new Java2sAutoTextField(v, jac);

    Container c = getContentPane();
    c.add(jac);
}

public static void main(String args[]){
   final Teste app = new Teste();
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Teste().setVisible(true);
        }
        
    });
}

}
//CODE

ateh ae tudo bem…soh que minha interface gráfica foi feita com o swing, sendo assim não consigo aplicar a ideia q usei no teste dentro do código pretendido, pq o tipo definido foi um JComboBox e defini-la como Java2sAutoComboBox esta impraticável pra mim!

Pf alguém pode me ajudar???

Podemos sim, mas antes de tudo, leia isso aqui:
http://www.guj.com.br/posts/list/50115.java

:wink:

agora acho q ta legal

[/code]
import java.util.;
import javax.swing.JTextField;
import javax.swing.text.
;

public class Java2sAutoTextField extends JTextField {
class AutoDocument extends PlainDocument {

    public void replace(int i, int j, String s, AttributeSet attributeset)
    throws BadLocationException {
        super.remove(i, j);
        insertString(i, s, attributeset);
    }
    
    public void insertString(int i, String s, AttributeSet attributeset)
    throws BadLocationException {
        if (s == null || "".equals(s))
            return;
        String s1 = getText(0, i);
        String s2 = getMatch(s1 + s);
        int j = (i + s.length()) - 1;
        if (isStrict && s2 == null) {
            s2 = getMatch(s1);
            j--;
        } else if (!isStrict && s2 == null) {
            super.insertString(i, s, attributeset);
            return;
        }
        if (autoComboBox != null && s2 != null)
            autoComboBox.setSelectedValue(s2);
        super.remove(0, getLength());
        super.insertString(0, s2, attributeset);
        setSelectionStart(j + 1);
        setSelectionEnd(getLength());
    }
    
    public void remove(int i, int j) throws BadLocationException {
        int k = getSelectionStart();
        if (k > 0)
            k--;
        String s = getMatch(getText(0, k));
        if (!isStrict && s == null) {
            super.remove(i, j);
        } else {
            super.remove(0, getLength());
            super.insertString(0, s, null);
        }
        if (autoComboBox != null && s != null)
            autoComboBox.setSelectedValue(s);
        try {
            setSelectionStart(k);
            setSelectionEnd(getLength());
        } catch (Exception exception) {
        }
    }
    
}

Java2sAutoTextField(Vector vector) {
    isCaseSensitive = false;
    isStrict = true;
    autoComboBox = null;
    if (vector == null) {
        throw new IllegalArgumentException("values can not be null");
    } else {
        dataVector = vector;
        init();
        return;
    }
}

public Java2sAutoTextField(Vector vector, Java2sAutoComboBox b) {
isCaseSensitive = false;
isStrict = true;
autoComboBox = null;
if (vector == null) {
throw new IllegalArgumentException(“values can not be null”);
} else {
dataVector = vector;
autoComboBox = b;
init();
return;
}
}

private void init() {
    setDocument(new AutoDocument());
    if (isStrict && dataVector.size() > 0)
        setText(dataVector.get(0).toString());
}

private String getMatch(String s) {
    for (int i = 0; i < dataVector.size(); i++) {
        String s1 = dataVector.get(i).toString();
        if (s1 != null) {
            if (!isCaseSensitive
                    && s1.toLowerCase().startsWith(s.toLowerCase()))
                return s1;
            if (isCaseSensitive && s1.startsWith(s))
                return s1;
        }
    }
    
    return null;
}

public void Selection(String s) {
    AutoDocument _lb = (AutoDocument) getDocument();
    if (_lb != null)
        try {
            int i = Math.min(getCaret().getDot(), getCaret().getMark());
            int j = Math.max(getCaret().getDot(), getCaret().getMark());
            _lb.replace(i, j - i, s, null);
        } catch (Exception exception) {
        }
}

public boolean isCaseSensitive() {
    return isCaseSensitive;
}

public void setCaseSensitive(boolean flag) {
    isCaseSensitive = flag;
}

public boolean isStrict() {
    return isStrict;
}

public void setStrict(boolean flag) {
    isStrict = flag;
}

public Vector getDataVector() {
    return dataVector;
}

public void setDataVector(Vector vector) {
    if (vector == null) {
        throw new IllegalArgumentException("values can not be null");
    } else {
        dataVector = vector;
        return;
    }
}

private Vector dataVector;

private boolean isCaseSensitive;

private boolean isStrict;

private Java2sAutoComboBox autoComboBox;

}

[/code]
import java.awt.event.ItemEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.plaf.basic.BasicComboBoxEditor;

public class Java2sAutoComboBox extends JComboBox {
private class AutoTextFieldEditor extends BasicComboBoxEditor {

    private Java2sAutoTextField getAutoTextFieldEditor() {
        return (Java2sAutoTextField) editor;
    }
    
    AutoTextFieldEditor(java.util.Vector vector) {
        editor = new Java2sAutoTextField(vector, Java2sAutoComboBox.this);
    }
}

public Java2sAutoComboBox(java.util.Vector vector) {
    isFired = false;
    autoTextFieldEditor = new AutoTextFieldEditor(vector);
    setEditable(true);
    setModel(new DefaultComboBoxModel(vector.toArray()) {
        
        protected void fireContentsChanged(Object obj, int i, int j) {
            if (!isFired)
                super.fireContentsChanged(obj, i, j);
        }
        
    });
    setEditor(autoTextFieldEditor);
}

public boolean isCaseSensitive() {
    return autoTextFieldEditor.getAutoTextFieldEditor().isCaseSensitive();
}

public void setCaseSensitive(boolean flag) {
    autoTextFieldEditor.getAutoTextFieldEditor().setCaseSensitive(flag);
}

public boolean isStrict() {
    return autoTextFieldEditor.getAutoTextFieldEditor().isStrict();
}

public void setStrict(boolean flag) {
    autoTextFieldEditor.getAutoTextFieldEditor().setStrict(flag);
}

public java.util.Vector getDataVector() {
    return autoTextFieldEditor.getAutoTextFieldEditor().getDataVector();
}

public void setDataVector(java.util.Vector vector) {
    autoTextFieldEditor.getAutoTextFieldEditor().setDataVector(vector);
    setModel(new DefaultComboBoxModel(vector.toArray()));
}

void setSelectedValue(Object obj) {
    if (isFired) {
        return;
    } else {
        isFired = true;
        setSelectedItem(obj);
        fireItemStateChanged(new ItemEvent(this, 701, selectedItemReminder, 1));
        isFired = false;
        return;
    }
}

protected void fireActionEvent() {
    if (!isFired)
        super.fireActionEvent();
}

private AutoTextFieldEditor autoTextFieldEditor;

private boolean isFired;

}

[code]

Desculpa amigo, acho que agora ficou melhor!
me da um help ae!

po q surra!!!

import java.util.*;
import javax.swing.JTextField;
import javax.swing.text.*;

public class Java2sAutoTextField extends JTextField  {
    class AutoDocument extends PlainDocument {
        
        public void replace(int i, int j, String s, AttributeSet attributeset)
        throws BadLocationException {
            super.remove(i, j);
            insertString(i, s, attributeset);
        }
        
        public void insertString(int i, String s, AttributeSet attributeset)
        throws BadLocationException {
            if (s == null || "".equals(s))
                return;
            String s1 = getText(0, i);
            String s2 = getMatch(s1 + s);
            int j = (i + s.length()) - 1;
            if (isStrict && s2 == null) {
                s2 = getMatch(s1);
                j--;
            } else if (!isStrict && s2 == null) {
                super.insertString(i, s, attributeset);
                return;
            }
            if (autoComboBox != null && s2 != null)
                autoComboBox.setSelectedValue(s2);
            super.remove(0, getLength());
            super.insertString(0, s2, attributeset);
            setSelectionStart(j + 1);
            setSelectionEnd(getLength());
        }
        
        public void remove(int i, int j) throws BadLocationException {
            int k = getSelectionStart();
            if (k > 0)
                k--;
            String s = getMatch(getText(0, k));
            if (!isStrict && s == null) {
                super.remove(i, j);
            } else {
                super.remove(0, getLength());
                super.insertString(0, s, null);
            }
            if (autoComboBox != null && s != null)
                autoComboBox.setSelectedValue(s);
            try {
                setSelectionStart(k);
                setSelectionEnd(getLength());
            } catch (Exception exception) {
            }
        }
        
    }
    
    Java2sAutoTextField(Vector vector) {
        isCaseSensitive = false;
        isStrict = true;
        autoComboBox = null;
        if (vector == null) {
            throw new IllegalArgumentException("values can not be null");
        } else {
            dataVector = vector;
            init();
            return;
        }
    }
    
  public Java2sAutoTextField(Vector vector, Java2sAutoComboBox b) {
        isCaseSensitive = false;
        isStrict = true;
        autoComboBox = null;
        if (vector == null) {
            throw new IllegalArgumentException("values can not be null");
        } else {
            dataVector = vector;
            autoComboBox = b;
            init();
            return;
        }
    }
    
    private void init() {
        setDocument(new AutoDocument());
        if (isStrict && dataVector.size() > 0)
            setText(dataVector.get(0).toString());
    }
    
    private String getMatch(String s) {
        for (int i = 0; i < dataVector.size(); i++) {
            String s1 = dataVector.get(i).toString();
            if (s1 != null) {
                if (!isCaseSensitive
                        && s1.toLowerCase().startsWith(s.toLowerCase()))
                    return s1;
                if (isCaseSensitive && s1.startsWith(s))
                    return s1;
            }
        }
        
        return null;
    }
    
    public void Selection(String s) {
        AutoDocument _lb = (AutoDocument) getDocument();
        if (_lb != null)
            try {
                int i = Math.min(getCaret().getDot(), getCaret().getMark());
                int j = Math.max(getCaret().getDot(), getCaret().getMark());
                _lb.replace(i, j - i, s, null);
            } catch (Exception exception) {
            }
    }
    
    public boolean isCaseSensitive() {
        return isCaseSensitive;
    }
    
    public void setCaseSensitive(boolean flag) {
        isCaseSensitive = flag;
    }
    
    public boolean isStrict() {
        return isStrict;
    }
    
    public void setStrict(boolean flag) {
        isStrict = flag;
    }
    
    public Vector getDataVector() {
        return dataVector;
    }
    
    public void setDataVector(Vector vector) {
        if (vector == null) {
            throw new IllegalArgumentException("values can not be null");
        } else {
            dataVector = vector;
            return;
        }
    }
    
    private Vector dataVector;
    
    private boolean isCaseSensitive;
    
    private boolean isStrict;
    
    private Java2sAutoComboBox autoComboBox;
}
import java.awt.event.ItemEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.plaf.basic.BasicComboBoxEditor;

public class Java2sAutoComboBox extends JComboBox {
    private class AutoTextFieldEditor extends BasicComboBoxEditor {
        
        private Java2sAutoTextField getAutoTextFieldEditor() {
            return (Java2sAutoTextField) editor;
        }
        
        AutoTextFieldEditor(java.util.Vector vector) {
            editor = new Java2sAutoTextField(vector, Java2sAutoComboBox.this);
        }
    }
    
    public Java2sAutoComboBox(java.util.Vector vector) {
        isFired = false;
        autoTextFieldEditor = new AutoTextFieldEditor(vector);
        setEditable(true);
        setModel(new DefaultComboBoxModel(vector.toArray()) {
            
            protected void fireContentsChanged(Object obj, int i, int j) {
                if (!isFired)
                    super.fireContentsChanged(obj, i, j);
            }
            
        });
        setEditor(autoTextFieldEditor);
    }
    
    public boolean isCaseSensitive() {
        return autoTextFieldEditor.getAutoTextFieldEditor().isCaseSensitive();
    }
    
    public void setCaseSensitive(boolean flag) {
        autoTextFieldEditor.getAutoTextFieldEditor().setCaseSensitive(flag);
    }
    
    public boolean isStrict() {
        return autoTextFieldEditor.getAutoTextFieldEditor().isStrict();
    }
    
    public void setStrict(boolean flag) {
        autoTextFieldEditor.getAutoTextFieldEditor().setStrict(flag);
    }
    
    public java.util.Vector getDataVector() {
        return autoTextFieldEditor.getAutoTextFieldEditor().getDataVector();
    }
    
    public void setDataVector(java.util.Vector vector) {
        autoTextFieldEditor.getAutoTextFieldEditor().setDataVector(vector);
        setModel(new DefaultComboBoxModel(vector.toArray()));
    }
    
    void setSelectedValue(Object obj) {
        if (isFired) {
            return;
        } else {
            isFired = true;
            setSelectedItem(obj);
            fireItemStateChanged(new ItemEvent(this, 701, selectedItemReminder, 1));
            isFired = false;
            return;
        }
    }
    
    protected void fireActionEvent() {
        if (!isFired)
            super.fireActionEvent();
    }
    
    private AutoTextFieldEditor autoTextFieldEditor;
    
    private boolean isFired;
    
}

Desculpa amigo, acho que agora ficou melhor!
me da um help ae!