Estou tentando criar um jComboBox.
Tenho um banco (CIDADES) com duas tabelas (CEP e ESTADOS).
Tabela (CEP ) com (ibge, cep , cidade e uf) ;
Tabela (ESTADO) com (código, sigla e nome);
Com o código abaixo , já consigo no primeiro jComboBox listar Sigla do estado e no segundo listar todas as cidades .
Como fazer para que quando eu escolher o estado no primeiro jComboBox , apenas as cidades pertencentes ao estado escolhido apareça no segundo ???
Já agradeço!!!
[code]public class JFrame2 extends JFrame implements ActionListener, KeyListener
{
private String url;
private String username;
private String password;
private Connection MinhaConexao = null;
private Statement MeuState = null;
private ResultSet rs;
private JTextField tfCargo;
private JComboBox jComboBoxEst, jComboBoxCid;
protected JPanel jPanel = new JPanel();
public JFrame2() throws HeadlessException
{
//conexão com o Banco de Dados
if (MinhaConexao == null){
try{
url = "jdbc:odbc:Cidades";
username= "";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection MinhaConexao = DriverManager.getConnection(url, username, password);
MeuState = MinhaConexao.createStatement();
}
catch(Exception ec){
if(ec.getMessage().equals("General error"))
JOptionPane.showMessageDialog(null, "Cadastro não pode ser efetuado");
else
JOptionPane.showMessageDialog(null, "Sem conexão com o BD"+"\n"+ec.getMessage(), "ERRO", JOptionPane.ERROR_MESSAGE);
}
}
jComboBoxEst = new javax.swing.JComboBox();
//tfCargo = new JTextField(15);
try{
String SQL="Select * From Estados";
rs=MeuState.executeQuery(SQL);
while(rs.next()){
jComboBoxEst.addItem(rs.getString("Sigla")); //lista todos os nomes no JComboBox
jComboBoxEst.setSelectedItem(null);
}
}catch(Exception a){}
jComboBoxCid = new javax.swing.JComboBox();
try{
String SQL="Select * From CEP";
rs=MeuState.executeQuery(SQL);
while(rs.next()){
jComboBoxCid.addItem(rs.getString("Cidade"));
jComboBoxCid.setSelectedItem(null);
}
}catch(Exception a){}
this.setDefaultCloseOperation(3);
this.setPreferredSize(new Dimension(640,480));
jPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
jComboBoxEst.setEditable(false);
jComboBoxCid.setEditable(false);
JTextComponent editor = (JTextComponent)jComboBoxEst.getEditor().getEditorComponent();
editor.addKeyListener(this);
jComboBoxEst.addActionListener(this);
jComboBoxEst.addKeyListener(this);
jComboBoxCid.addActionListener(this);
jComboBoxCid.addKeyListener(this);
jPanel.add(jComboBoxEst);
jPanel.add(jComboBoxCid);
Container container = this.getContentPane();
container.add(jPanel);
}
public static void main(String[] args)
{
JFrame2 fs = new JFrame2();
fs.pack();
fs.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == 10)
{
System.out.println ( jComboBoxEst.getSelectedItem().toString());
if(jComboBoxEst.getSelectedItem().toString()!=null){
try{
String SQL="Select UF, Cidades From CEP where Sigla='"+jComboBoxEst.getSelectedItem().toString()+"'";
rs=MeuState.executeQuery(SQL);
while(rs.next()){
tfCargo.setText(rs.getString("Codigo"));
}
}catch(Exception a){}
}
}
}
public void keyTyped(KeyEvent e)
{
}
}
[/code]