Autocomplete em combobox

1 resposta
C

Gente…

Estou a fazer um programa que usa JMenuItens

Em alguns menus uso AutoComplete’s da seguinte maneira:

Crio uma classe para o código dos autoComplete:

package FuncoesGerais;

import BaseDados_Ligacao.CriaConexao;
import Logica_Gets_e_Sets.VALOR_APAGAR;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.swing.AutoCompleteSupport;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JTextArea;

/**
 *
 * @author Luis Castro
 */
public class funcoesAutoComplete
{
    AutoCompleteSupport nomesAutoComplete;
    AutoCompleteSupport codigosAutoComplete1;
    AutoCompleteSupport Combo1AutoComplete;
    AutoCompleteSupport Combo2AutoComplete;
            
    private Connection conexao;
    
    EventList Listacodigo = new BasicEventList();
    EventList ListaNomes = new BasicEventList();
    EventList ListaDadosCombo1 = new BasicEventList();
    EventList ListaDadosCombo2 = new BasicEventList();
    EventList ListaDadosCombo3 = new BasicEventList();
    EventList ListaValor = new BasicEventList();
    EventList ListaDescricao = new BasicEventList();
    
    public String campoa;
    public String campob;
    public String campoc;
    public boolean instalado=false;

    public funcoesAutoComplete()
    {
        try {
            this.conexao = CriaConexao.getConexao();
        } catch (SQLException ex) {
            Logger.getLogger(funcoesAutoComplete.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //para ComboBox's  //usado
    public Object listacampo_deTabela_porValorde_OutroCampo(String nomecampo_a_Listar, String tabelaCampo, String CampoK_impoeCondicao, String tipoPessoa, Object condicao, boolean PesquisardCodparaNome, boolean Ute_i_Atle) throws SQLException
    {
        String sql = null;
        
        if(Ute_i_Atle == false){ sql = "select "+nomecampo_a_Listar+" from " +tabelaCampo+ " order by " +CampoK_impoeCondicao; }
        if(Ute_i_Atle == true && !tipoPessoa.equals("nulo")){ sql = "select "+tabelaCampo+"."+nomecampo_a_Listar+" from " +tabelaCampo+ ", "+tipoPessoa+" where "+tabelaCampo+".id_pessoa = "+tipoPessoa+".id_pessoa and "+tabelaCampo+"."+CampoK_impoeCondicao+" = '"+condicao+"' order by  "+tabelaCampo+"."+CampoK_impoeCondicao; }
        if(Ute_i_Atle == true && tipoPessoa.equals("nulo")){ sql = "select "+tabelaCampo+"."+nomecampo_a_Listar+" from " +tabelaCampo+ " where  "+tabelaCampo+"."+CampoK_impoeCondicao+" = '"+condicao+"' order by  "+tabelaCampo+"."+CampoK_impoeCondicao; }
        
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        
        ResultSet rs = stmt.executeQuery(sql);
        EventList a = new BasicEventList();

        if(PesquisardCodparaNome == true)
        {
            while(rs.next())
            {
                a.add(rs.getString(1));
            }
        }
        if(PesquisardCodparaNome == false)
        {
            while(rs.next())
            {
                a.add(rs.getString(1));
            }
        }

        rs.close();
        stmt.close();

        Object b=null;
        
        if(a.size() == 1)
        {
            b = a.get(0);
        }
        
        return b;
    }

//método preenche eventList com dados da BD

    //para ComboBox's //usado
    public EventList AutoCompleteComboBox(String campoSelec, String tabela, String orderBy, String tipoPessoa, String chave_Estrangeira, 
            boolean EventList_campo1, boolean Ute_i_Atle) throws SQLException
    {
        String sql = "";
        
        if(Ute_i_Atle == true){ sql = "select "+campoSelec+" from " +tabela+ " order by " +orderBy; }
        if(Ute_i_Atle == false){ sql = "select "+tabela+"."+campoSelec+" from " +tabela+ ", "+tipoPessoa+" where "+tabela+"."+chave_Estrangeira+" = "+tipoPessoa+".id_pessoa order by "+tabela+"."+orderBy; }
        if(tipoPessoa != null && tipoPessoa.equals("epocas"))
        {
            sql = "select distinct "+campoSelec+" from "+tabela+" order by "+orderBy+" desc";
        }
        if(tipoPessoa != null && tipoPessoa.equals("grupos"))
        {
            sql = "select distinct nome from pessoa, grupo where pessoa.id_pessoa = grupo.id_pessoa";
        }
        
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        System.out.println(sql);
        ResultSet rs = stmt.executeQuery(sql);
        EventList a = new BasicEventList();

        if (EventList_campo1 == true)
        {
            while(rs.next())
            {
                a.add(rs.getString(1));
            }
        }
        
        if (EventList_campo1 == false)
        {
            while(rs.next())
            {
                a.add(rs.getString(1));
            }
        }

        rs.close();
        stmt.close();
        
        return a;
    }
    
//. . . . . . .
//método instala autocomplete na combobox

    public void completaAutoComboPesquisa1Combo(String campo1, String tabela, final JComboBox jComboBox1, final String tipoPessoa, 
            final String chave_Estrangeira, final boolean Ute_i_Atle, boolean actualizar) throws SQLException
    {
        jComboBox1.revalidate();
        
        ListaDadosCombo1 = AutoCompleteComboBox(campo1, tabela, campo1, tipoPessoa, chave_Estrangeira, true, Ute_i_Atle);

        Combo1AutoComplete = AutoCompleteSupport.install(jComboBox1, ListaDadosCombo1);
    }

//. . . . . .

depois nos panels pretendidos chamo o método para o autocomplete:

Tudo funnciona bem, só que eu agora preciso actualizar os dados da List que recebo da combo que é usada no autocomplete, sempre que insiro novos dados na combo!

Como posso fazê-lo?

1 Resposta

ViniGodoy

Movido para o fórum de interface gráfica.

Criado 8 de agosto de 2011
Ultima resposta 8 de ago. de 2011
Respostas 1
Participantes 2