Ola,
Estou tentando fazer um componente que o cidadao digita parte do nome no JTextLookupCombo, filho de JComboBox, e ele preenche na hora o combo (popupmenu) filtrando no BD somente os registros encontrados.
Esta “quase” funcionando… o unico problema é que, quando existe mais de um registro, eu quero mostrar o popupmenu para o usuario escolher, mas nao funciona! Nao sei se o problema é porque esta dentro do actionPerformed que ele se perde…
O codigo do componente esta abaixo:
package SubMacroForms;
/*
* TextLookupCombo.java
*
* Created on 11 de Maio de 2006, 15:52
*
* Swing Visual Component
*
*/
import java.sql.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.text.*;
import SubMacroUtils.Erro;
import SubMacroUtils.Geral;
import SubMacroForms.GeralForms;
/**
*
* @author Edilmar Alves
*/
public class JTextLookupCombo extends JComboBox implements ActionListener {
Connection connLocal;
String lookupsql, lookupfield, lookupdisplay;
String textoAtual;
/** Creates a new instance of TextLookupCombo */
public JTextLookupCombo(Connection connLocal, String lookupsql, String lookupfield, String lookupdisplay) {
super();
init(connLocal, lookupsql, lookupfield, lookupdisplay);
}
void init(Connection connLocal, String lookupsql, String lookupfield, String lookupdisplay) {
this.connLocal = connLocal;
this.lookupsql = lookupsql;
this.lookupfield = lookupfield;
this.lookupdisplay = lookupdisplay;
//
setEditable(true);
//
addActionListener(this);
}
public Connection getconnLocal() { return connLocal; }
public String getlookupsql() { return lookupsql; }
public String getlookupfield() { return lookupfield; }
public String getlookupdisplay() { return lookupdisplay; }
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof ComboBoxEditor) {
// ao pressionar ENTER dentro do componente, este evento é ativado
textoAtual = ((ComboBoxEditor)e.getSource()).getItem().toString();
if (textoAtual.equals("")) {
int resp = JOptionPane.showConfirmDialog(this, "Deseja fazer uma pesquisa geral?", "Aviso", JOptionPane.YES_NO_OPTION);
if (resp == JOptionPane.YES_OPTION)
pesquisar();
} else
pesquisar();
}
}
public void pesquisar() {
//JOptionPane.showMessageDialog(this, "Teste...", "Mensagem", JOptionPane.INFORMATION_MESSAGE);
removeAllItems();
try {
String item, SQL = getlookupsql();
SQL = Geral.TrocaSubstring(SQL,"?","'%" + textoAtual + "%'");
PreparedStatement pstmt = getconnLocal().prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
item = rs.getString(getlookupdisplay());
addItem(item);
}
rs.close();
pstmt.close();
if (getItemCount() == 0) {
Erro.TratarErro(Erro.PORTUGUES,
Erro.ERRO_ABO, Erro.ERRO_INF_CONS_VAZIOF, Erro.ACAO_CONS_DIG_NOV,
new String[] {"Pesquisa",""}, new String[] {""}, 0);
requestFocus();
} else {
requestFocus();
showPopup(); // NAO FUNCIONA!!!
}
} catch (Exception e) {
e.printStackTrace();
Erro.TratarErro(Erro.PORTUGUES,
Erro.ERRO_ABO, Erro.ERRO_ABO_BD_FIND_TABLE, Erro.ACAO_BD_CONTAC_ADMIN,
new String[] {""}, new String[] {""}, 0);
}
}
}