ola pessoal …estou tentando aprender um pouco de java …e preciso de mais uma ajuda…
estou fazendo uma tela na qual eu tenho um jcombobox que lista os nomes que tenho em um banco de dados… no restante da tela eu tenho jtextfields (rg, cic. endereço…) que serão atualizados com as informações correspondentes ao nome selecionado no jcombo box.
como eu faço para atualizar os campos jtextfields??
ai vai o meu codigo …espero q facilite…
[code]import java.sql.*;
import javax.swing.ImageIcon;
import javax.swing.*;
import java.awt.;
import java.awt.event.;
class consultaDiscente extends JFrame
{
JLabel jLabel1,jLabel2,jLabel3,jLabel4,jLabel5,jLabel6,jLabel7,
jLabel8,jLabel9,jLabel10,jLabel11,jLabel12,jLabel13;
JTabbedPane abas;
JComboBox jcNome;
static JTextField tfEndereco, tfCidade, tfCEP, tfEstado,
tfEmail, tfFone, tfDataNasc, tfNacionalidade, tfNaturalidade, tfRg,
tfCic;
JPanel p1 = new JPanel();
ImageIcon icone;
ResultSet rs;
Statement MeuState;
Font f1 = new Font (“Comic Sans MS”,Font.BOLD,24);
Font f2 = new Font (“Comic Sans MS”,Font.BOLD,12);
public static void main(String args[])
{
JFrame Janela = new consultaDiscente();
Janela.show();
WindowListener x = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
Janela.addWindowListener(x);
}
consultaDiscente()
{
jLabel1 = new JLabel();
jLabel2 = new JLabel();
abas = new JTabbedPane();
jcNome = new JComboBox();
icone = new ImageIcon();
p1.setLayout(null);
JComponent cadastro = abaCadastro("Panel #1");
abas.addTab("Cadastro",icone,cadastro,"Does nothing");
abas.setMnemonicAt(0, KeyEvent.VK_C);
/* JComponent estagio = makeTextPanel(“Panel #2”);
abas.addTab(“Estagio”,icone,estagio,“Does nothing”);
abas.setMnemonicAt(1, KeyEvent.VK_E);
JComponent mestrado = makeTextPanel("Panel #3");
abas.addTab("Mestrado",icone,mestrado,"Does nothing");
abas.setMnemonicAt(2, KeyEvent.VK_M);
JComponent doutorado = makeTextPanel("Panel #2");
abas.addTab("Doutorado",icone,doutorado,"Does nothing");
abas.setMnemonicAt(3, KeyEvent.VK_D);
JComponent pos_doc = makeTextPanel("Panel #2");
abas.addTab("Pós-Doc",icone,pos_doc,"Does nothing");
abas.setMnemonicAt(4, KeyEvent.VK_P);*/
abas.setBounds(10, 100, 450, 350);
add(abas);
jLabel1.setText(“Consulta Discente”);
jLabel1.setFont(f1);
p1.add(jLabel1);
jLabel1.setBounds(70, 10, 300, 23);
jLabel2.setText(“Nome:”);
jLabel2.setFont(f2);
p1.add(jLabel2);
jLabel2.setBounds(30, 40, 160, 23);
p1.add(jcNome);
jcNome.setBounds(30, 60, 370, 27);
p1.setBackground(new Color(144,238,144));
getContentPane().add(p1, java.awt.BorderLayout.CENTER);
setTitle(“Cadastramento de Discentes”);
setSize(480,480);
setResizable(false);
String url = “jdbc:odbc:datacena”;
try
{
Class.forName( “sun.jdbc.odbc.JdbcOdbcDriver” );
Connection MinhaConexao = DriverManager.getConnection(url);
MeuState = MinhaConexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = MeuState.executeQuery(“SELECT Nome FROM Cadastro ORDER BY Nome”);
rs = MeuState.getResultSet();
while (rs.next()){
jcNome.addItem(rs.getString(“Nome”));
}
}
catch(ClassNotFoundException ex)
{
System.out.println(“Driver JDBC-ODBC não encontrado!”);
}
catch(SQLException ex)
{
System.out.println(“Problemas na conexao com a fonte de dados”);
}
}
protected JComponent abaCadastro(String text) {
JPanel cadastro = new JPanel(false);
cadastro.setLayout(null);
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jLabel6 = new JLabel();
jLabel7 = new JLabel();
jLabel8 = new JLabel();
jLabel9 = new JLabel();
jLabel10 = new JLabel();
jLabel11 = new JLabel();
jLabel12 = new JLabel();
jLabel13 = new JLabel();
tfCEP = new JTextField();
tfEstado = new JTextField();
tfEmail = new JTextField();
tfFone = new JTextField();
tfDataNasc = new JTextField();
tfNacionalidade = new JTextField();
tfNaturalidade = new JTextField();
tfRg = new JTextField();
tfCic = new JTextField();
tfEndereco = new JTextField();
tfCidade = new JTextField();
jLabel3.setText("Rg:");
jLabel3.setFont(f2);
cadastro.add(jLabel3);
jLabel3.setBounds(30, 10, 20, 23);
cadastro.add(tfRg);
tfRg.setBounds(30, 30, 120, 27);
jLabel4.setText("CIC:");
jLabel4.setFont(f2);
cadastro.add(jLabel4);
jLabel4.setBounds(165, 10, 30, 23);
cadastro.add(tfCic);
tfCic.setBounds(165, 30, 120, 27);
jLabel5.setText("Data Nascimento:");
jLabel5.setFont(f2);
cadastro.add(jLabel5);
jLabel5.setBounds(300, 10, 150, 23);
cadastro.add(tfDataNasc);
tfDataNasc.setBounds(300, 30, 100, 27);
jLabel6.setText("Endereço:");
jLabel6.setFont(f2);
cadastro.add(jLabel6);
jLabel6.setBounds(30, 60, 80, 23);
cadastro.add(tfEndereco);
tfEndereco.setBounds(30, 80, 370, 27);
jLabel7.setText("Cidade:");
jLabel7.setFont(f2);
cadastro.add(jLabel7);
jLabel7.setBounds(30, 110, 48, 23);
cadastro.add(tfCidade);
tfCidade.setBounds(30, 130, 180, 27);
jLabel8.setText("UF:");
jLabel8.setFont(f2);
cadastro.add(jLabel8);
jLabel8.setBounds(225, 110, 42, 23);
cadastro.add(tfEstado);
tfEstado.setBounds(225, 130, 30, 27);
jLabel9.setText("CEP:");
jLabel9.setFont(f2);
cadastro.add(jLabel9);
jLabel9.setBounds(270, 110, 26, 23);
cadastro.add(tfCEP);
tfCEP.setBounds(270, 130, 130, 27);
jLabel10.setText("Fone:");
jLabel10.setFont(f2);
cadastro.add(jLabel10);
jLabel10.setBounds(30, 160, 48, 23);
cadastro.add(tfFone);
tfFone.setBounds(30, 180, 120, 27);
jLabel11.setText("E-mail:");
jLabel11.setFont(f2);
cadastro.add(jLabel11);
jLabel11.setBounds(165, 160, 50, 23);
cadastro.add(tfEmail);
tfEmail.setBounds(165, 180, 235, 27);
jLabel12.setText("Naturalidade:");
jLabel12.setFont(f2);
cadastro.add(jLabel12);
jLabel12.setBounds(30, 210, 80, 23);
cadastro.add(tfNaturalidade);
tfNaturalidade.setBounds(30, 230, 370, 27);
jLabel13.setText("Nacionalidade:");
jLabel13.setFont(f2);
cadastro.add(jLabel13);
jLabel13.setBounds(30, 260, 90, 23);
cadastro.add(tfNacionalidade);
tfNacionalidade.setBounds(30, 280, 370, 27);
return cadastro;
}
public void atualizaCampos()
{
try
{
tfRg.setText(rs.getString("Rg"));
tfCic.setText(rs.getString("Cic"));
tfEndereco.setText(rs.getString("Endereco"));
tfCEP.setText(rs.getString("Cep"));
tfCidade.setText(rs.getString("Cidade"));
tfEstado.setText(rs.getString("Estado"));
tfEmail.setText(rs.getString("Email"));
tfFone.setText(rs.getString("Telefone"));
tfDataNasc.setText(""+rs.getString("Data_nascimento"));
tfNaturalidade.setText(rs.getString("Naturalidade"));
tfNacionalidade.setText (rs.getString("Nacionalidade"));
}
catch(SQLException ex)
{ }
}
}[/code]
eu tentei por um sql no meio …mais não deu certo.
abraço :lol: