Pessoal estou tentando fazer uma atualização de ComboBox mas qd rodo não dá nenhum erro só não atualiza.
o codigo é o seguinte:
Nesta parte a pessoa tem q escolher uma cidade e dependendo da cidade escolhida o proximo ComboBox será atualizado e terá q mostrar apenas os medicos daquela cidade.
JPanel painel2 = new JPanel();
painel2.setLayout(new GridLayout(2,1));
painel2.add(new JLabel("*Cidade:", SwingConstants.LEFT));
cid = new JComboBox();
String[] cidades = { " ", "1 - Santos", "2 - São Vicente", "3 - Guarujá", "4 - Vicente de Carvalho", "5 - Praia Grande", "6 - Cubatão", " 7 - Mongaguá", "8 - Bertioga"};//seleciona as cidades que podem estar no roteiro.
cid = new JComboBox(cidades);
cid.addActionListener(this);
cid.setSelectedItem(" ");
texto1 = new JLabel();
updateLabel1(cidades[cid.getSelectedIndex()]);// Chama o metodo para atualizar o label Tempo de Movimenteção.
painel2.add(cid);
esta parte é a declaração do combo no frame
JPanel painel4 = new JPanel();
painel4.setLayout(new GridLayout(2,1));
painel4.add(new JLabel("*Médico 01:", SwingConstants.LEFT));
med01 = new JComboBox();
texto = new JLabel();
painel4.add(med01);
aqui é o evento q acontecerá qd o usuario escolher a cidade
cid.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
texto1.setText(String.valueOf(cid.getSelectedItem()));
String cidade;
String cidad = "";
cidade = (String)cid.getSelectedItem();
int i = 0;
while (cidade.charAt(i) != ' '){//pegar só o codigo
cidad = cidad + cidade.charAt(i);
i++;
}
med01.removeAllItems();//remove o que tem nos outros combobox
med02.removeAllItems();
med03.removeAllItems();
med04.removeAllItems();
med05.removeAllItems();
med06.removeAllItems();
med07.removeAllItems();
med08.removeAllItems();
med09.removeAllItems();
med10.removeAllItems();
med11.removeAllItems();
med12.removeAllItems();
med13.removeAllItems();
med14.removeAllItems();
med01.addItem("");
med02.addItem("");
med03.addItem("");
med04.addItem("");
med05.addItem("");
med06.addItem("");
med07.addItem("");
med08.addItem("");
med09.addItem("");
med10.addItem("");
med11.addItem("");
med12.addItem("");
med13.addItem("");
med14.addItem("");
try{// seleciona os médicos da cidade selecionada
Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost/roteiros","root","root");
PreparedStatement sql = conexao.prepareStatement("SELECT * FROM medico where cid = ?");
sql.setString(1, (String) cid.getSelectedItem());
ResultSet res = sql.executeQuery();
while(res.next()){
med01.addItem(res.getString("cod") + " - " + res.getString("nome"));
updateLabel(med01.getSelectedIndex());
}
}
catch (SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Não foi possível conectar com o banco!","Erro",0);
}
}
});
e aki é a parte qd o usuario escolher o medico
med01.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int i = 0;
String medico1 = "";
String med1 = (String)med01.getSelectedItem();
if (med1 == null){
med1 = "0 ";
}
if (!med1.equals("")){
while (med1.charAt(i) != ' '){
medico1 = medico1 + med1.charAt(i);
i++;
}
med02.removeAllItems();
med03.removeAllItems();
med04.removeAllItems();
med05.removeAllItems();
med06.removeAllItems();
med07.removeAllItems();
med08.removeAllItems();
med09.removeAllItems();
med10.removeAllItems();
med11.removeAllItems();
med12.removeAllItems();
med13.removeAllItems();
med14.removeAllItems();
med02.addItem("");
med03.addItem("");
med04.addItem("");
med05.addItem("");
med06.addItem("");
med07.addItem("");
med08.addItem("");
med09.addItem("");
med10.addItem("");
med11.addItem("");
med12.addItem("");
med13.addItem("");
med14.addItem("");
tempo_med.setText("");
try{
Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost//roteiros","root","root");
PreparedStatement sql = conexao.prepareStatement("SELECT * FROM medico where cid = ?");
sql.setString(1, (String) cid.getSelectedItem());
ResultSet res = sql.executeQuery();
while(res.next()){
med02.addItem(res.getString("cod") + " - " + res.getString("nome"));
updateLabel(med02.getSelectedIndex());
}
}
catch (SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Não foi possível conectar com o banco!","Erro",0);
}
}
}
});
Se alguem puder me ajudar ficarei muito agradecida
:roll: :?: