Ola. Gosto de quebrar a cabeca antes de pedir ajuda, mas…
Tenho a classe motorista. Nela tem um button que chama a classe ListaPlaca que associa um onibus ao motorista. Blza?
O que quero fazer e’ o seguinte: quando selecionar a placa no JList da classe ListaPlaca, em tempo de execucao “setar” o JTextField na classe motorista que ja estava aberta anteriormente.
Segue o codigo;
Classe Motorista:
package Tela;
import banco.MotoristaDAO;
import cadastros.Motoristas;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class CadMotorista extends javax.swing.JFrame{
// /** Creates new form CadMotorista */
public CadMotorista(){
initComponents();
}
public void setTfOnibus(String s)
{
tfOnibus.setText(s);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Código Gerado ">
private void initComponents() {
jlNomeMot = new javax.swing.JLabel();
jlCpf = new javax.swing.JLabel();
jlDtNasc = new javax.swing.JLabel();
jlHabilit = new javax.swing.JLabel();
jlCatHabil = new javax.swing.JLabel();
tfNomeMot = new javax.swing.JTextField();
tfCpf = new javax.swing.JTextField();
tfDtNasc = new javax.swing.JTextField();
tfHabilit = new javax.swing.JTextField();
tfCatHabil = new javax.swing.JTextField();
btSalvar = new javax.swing.JButton();
btLimpar = new javax.swing.JButton();
btSair = new javax.swing.JButton();
jlOnibus = new javax.swing.JLabel();
tfOnibus = new javax.swing.JTextField();
btLista = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Cadastro de Motoristas");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jlNomeMot.setText("Nome");
jlCpf.setText("CPF");
jlDtNasc.setText("Data de Nascimento");
jlHabilit.setText("Habilitacao");
jlCatHabil.setText("Categoria");
btSalvar.setText("Salvar");
btSalvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSalvarActionPerformed(evt);
}
});
btLimpar.setText("Limpar");
btLimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btLimparActionPerformed(evt);
}
});
btSair.setText("Sair");
btSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSairActionPerformed(evt);
}
});
jlOnibus.setText("Onibus");
btLista.setText("...");
btLista.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btListaActionPerformed(evt);
}
});
...
pack();
}// </editor-fold>
private void btListaActionPerformed(java.awt.event.ActionEvent evt) {
try {
new ListaPlaca().setVisible(true);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {
Limpar();
}
private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {
Motoristas motorista = new Motoristas();
//JFormattedTextField tfDtNasc = new JFormattedTextField((setMascara("##/##/####")));
String NomeMot= tfNomeMot.getText();
String Cpf = tfCpf.getText();
String DtNasc = tfDtNasc.getText();
String Habilit = tfHabilit.getText();
String Cathabil = tfCatHabil.getText();
String Onibus = tfOnibus.getText();
Double Cpfd = Double.valueOf(Cpf).doubleValue();
motorista.setNomemot(NomeMot);
motorista.setCpf(Cpfd);
motorista.setDtnasc(DtNasc);
motorista.setHabilitacao(Habilit);
motorista.setCathabilit(Cathabil);
motorista.setOnibus(Onibus);
MotoristaDAO mot;
try {
mot = new MotoristaDAO();
mot.adiciona(motorista);
} catch (SQLException ex) {
Logger.getLogger(CadMotorista.class.getName()).log(Level.SEVERE, null, ex);
}
Limpar();
}
private void btSairActionPerformed(java.awt.event.ActionEvent evt) {
CadMotorista.this.dispose();
}
private void Limpar() {
tfNomeMot.setText("");
tfCpf.setText("");
tfDtNasc.setText("");
tfHabilit.setText("");
tfCatHabil.setText("");
tfOnibus.setText("");
}
/**private MaskFormatter setMascara(String mascara) {
* MaskFormatter mask = null;
* try{
* mask = new MaskFormatter(mascara);
* }catch(java.text.ParseException ex){}
* return mask;
*}
*/
Agora a classe JTable
package Tela;
import banco.ConnectionFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import Tela.CadMotorista;
public class ListaPlaca extends JFrame{
JList lista;
List placas;
public String s;
JButton btnSelecionar = new JButton("Selecionar");
private Connection connection;
public ListaPlaca() throws SQLException{
this.connection=ConnectionFactory.getConnection();
PreparedStatement stmt = connection.prepareStatement("select placas from onibus");
ResultSet rs = stmt.executeQuery();
placas = new ArrayList();
while(rs.next())
{
placas.add(rs.getString(1));
}
Container tela = getContentPane();
FlowLayout layout = new FlowLayout();
tela.setLayout(layout);
Tratador trat = new Tratador();
lista = new JList();
lista.setListData(placas.toArray());
lista.setVisibleRowCount(5);
lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane painelRolagem = new JScrollPane(lista);
btnSelecionar.addActionListener(trat);
tela.add(painelRolagem);
tela.add(btnSelecionar);
pack();
setVisible(true);
}
private class Tratador implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
CadMotorista cad = new CadMotorista();
//String s[]=(placas[lista.getSelectedIndex()]);
s = lista.getSelectedValue().toString();
//teste.setText(s);
System.out.println(s);
//cad.setTfOnibus(s);
}
}
}
Como podem ver, tentei ate’ criar um metodo setTfOnibus que e’ o JTextField que quero alterar.
Deem uma dica, por favor!