Minha não é nova, porém os tópicos anteriores não conseguiram me ajudar.
Eu tenho dados de Aluno os quais são carregados dentro da tabela depois de uma busca, quero setar as informações da tabela nos respectivos campos do formulário, o único que não consegui foi os relativos ao JCombobox.
Deem uma olhada no método setarCampos.
package Templates;
import Classes.Aluno;
import Classes.Curso;
import Classes.Turma;
import DAO.AlunoDAO;
import DAO.CursoDAO;
import DAO.TurmaDAO;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
import java.sql.ResultSet;
public class AlunoForm extends javax.swing.JInternalFrame
{
public AlunoForm()
{
initComponents();
}
public void buscar(String termo)
{
try
{
DefaultTableModel modelo = (DefaultTableModel)tblAluno.getModel();
modelo.setNumRows(0);
modelo.setColumnIdentifiers(new Object[]{
"Código", "Nome", "Email", "Telefone", "Endereço", "Data",
"CPF", "RG", "Sexo", "Turma","Curso", });
AlunoDAO dao = new AlunoDAO();
for (Aluno a:dao.BuscarAluno(termo))
{
modelo.addRow(new Object[]{
a.getIdAluno(),
a.getNomeAluno(),
a.getEmailAluno(),
a.getTelAluno(),
a.getEnderecoAluno(),
a.getDataNascimentoAluno(),
a.getCpfAluno(),
a.getRgAluno(),
a.getSexoAluno(),
a.getTurmaAluno(),
a.getCursoAluno(),
});
}
}
catch (Exception e)
{
}
btnExcluir.setEnabled(true);
btnEditar.setEnabled(true);
btnAdicionar.setEnabled(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField8 = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
txtNomeAluno = new javax.swing.JTextField();
txtEmailAluno = new javax.swing.JTextField();
txtEnderecoAluno = new javax.swing.JTextField();
btnEditar = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblAluno = new javax.swing.JTable();
btnExcluir = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
btnAdicionar = new javax.swing.JButton();
txtTelAluno = new javax.swing.JFormattedTextField();
txtDataNascimentoAluno = new javax.swing.JFormattedTextField();
comboSexo = new javax.swing.JComboBox<>();
txtCpfAluno = new javax.swing.JFormattedTextField();
comboCurso = new javax.swing.JComboBox();
txtRgAluno = new javax.swing.JFormattedTextField();
comboTurma = new javax.swing.JComboBox();
txtPesquisarAluno = new javax.swing.JTextField();
btnBuscarAluno = new javax.swing.JButton();
jTextField8.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jTextField8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
jPanel1.setBackground(new java.awt.Color(102, 153, 255));
jLabel1.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel1.setText("Nome:");
jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel2.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel2.setText("E-mail:");
jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel3.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel3.setText("Data de Nascimento:");
jLabel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel4.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel4.setText("Tel;");
jLabel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel5.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel5.setText("Endereço:");
jLabel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel6.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel6.setText("CPF:");
jLabel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel7.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel7.setText("RG:");
jLabel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel8.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel8.setText("Turma:");
jLabel8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel9.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel9.setText("Sexo:");
jLabel9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel10.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel10.setText("Curso:");
jLabel10.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
txtNomeAluno.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
txtNomeAluno.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
txtEmailAluno.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
txtEmailAluno.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
txtEnderecoAluno.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
txtEnderecoAluno.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
btnEditar.setBackground(new java.awt.Color(51, 51, 51));
btnEditar.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
btnEditar.setForeground(new java.awt.Color(255, 255, 255));
btnEditar.setText("Editar");
btnEditar.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnEditar.setEnabled(false);
btnEditar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditarActionPerformed(evt);
}
});
tblAluno.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
tblAluno.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tblAlunoMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tblAluno);
btnExcluir.setBackground(new java.awt.Color(51, 51, 51));
btnExcluir.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
btnExcluir.setForeground(new java.awt.Color(255, 255, 255));
btnExcluir.setText("Excluir");
btnExcluir.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnExcluir.setEnabled(false);
jButton3.setBackground(new java.awt.Color(51, 51, 51));
jButton3.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jButton3.setForeground(new java.awt.Color(255, 255, 255));
jButton3.setText("Limpar");
jButton3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
btnAdicionar.setBackground(new java.awt.Color(51, 51, 51));
btnAdicionar.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
btnAdicionar.setForeground(new java.awt.Color(255, 255, 255));
btnAdicionar.setText("Adicionar");
btnAdicionar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
btnAdicionar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAdicionarActionPerformed(evt);
}
});
try {
txtTelAluno.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)#####-####")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
try {
txtDataNascimentoAluno.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
comboSexo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " ", "Masculino", "Feminino" }));
try {
txtCpfAluno.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
comboCurso.addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
comboCursoAncestorAdded(evt);
}
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
}
});
try {
txtRgAluno.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###-###-##.##")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
comboTurma.addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
comboTurmaAncestorAdded(evt);
}
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
}
});
txtPesquisarAluno.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
btnBuscarAluno.setBackground(new java.awt.Color(51, 51, 51));
btnBuscarAluno.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
btnBuscarAluno.setForeground(new java.awt.Color(255, 255, 255));
btnBuscarAluno.setText("Buscar Aluno:");
btnBuscarAluno.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnBuscarAluno.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBuscarAlunoActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtNomeAluno)
.addComponent(txtEmailAluno)
.addComponent(comboCurso, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtTelAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(txtDataNascimentoAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel9)
.addGap(18, 18, 18)
.addComponent(comboSexo, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel8)
.addGap(18, 18, 18)
.addComponent(comboTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtEnderecoAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtCpfAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtRgAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
.addComponent(jScrollPane1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(btnAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(114, 114, 114)
.addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(150, 150, 150)
.addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 117, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(72, 72, 72))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(230, 230, 230)
.addComponent(btnBuscarAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtPesquisarAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtNomeAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(jLabel9)
.addComponent(txtTelAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(txtDataNascimentoAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(comboSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel5)
.addComponent(jLabel6)
.addComponent(txtCpfAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7)
.addComponent(txtRgAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(txtEmailAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtEnderecoAluno, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel10)
.addComponent(jLabel8)
.addComponent(comboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(comboTurma, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(txtPesquisarAluno)
.addGap(1, 1, 1))
.addComponent(btnBuscarAluno))
.addGap(81, 81, 81)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(32, 32, 32))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnAdicionarActionPerformed(java.awt.event.ActionEvent evt) {
try
{
AlunoDAO alunodao = new AlunoDAO();
Aluno aluno = new Aluno();
aluno.setNomeAluno(txtNomeAluno.getText());
aluno.setEmailAluno(txtEmailAluno.getText());
aluno.setTelAluno(txtTelAluno.getText());
SimpleDateFormat sdfInput = new SimpleDateFormat("dd/MM/yyyy");
try
{
String dataNascimentoInput = txtDataNascimentoAluno.getText().trim();
if (!dataNascimentoInput.isEmpty())
{
Date utilDate = sdfInput.parse(dataNascimentoInput);
java.sql.Date sqlDateFormatted = new java.sql.Date(utilDate.getTime());
aluno.setDataNascimentoAluno(sqlDateFormatted);
}
else
{
System.err.println("Erro: Data de nascimento inválida.");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Erro: Formato de data inválido.");
}
aluno.setEnderecoAluno(txtEnderecoAluno.getText());
aluno.setRgAluno(txtRgAluno.getText());
aluno.setCpfAluno(txtCpfAluno.getText());
aluno.setSexoAluno((String) comboSexo.getSelectedItem());
Turma turmaSelecionada = (Turma) comboTurma.getSelectedItem();
if (turmaSelecionada != null)
{
aluno.setTurmaAluno(turmaSelecionada.getIdTurma());
}
else
{
JOptionPane.showMessageDialog(null, "Erro: Nenhuma turma selecionada.");
}
Curso cursoSelecionado = (Curso) comboCurso.getSelectedItem();
if (cursoSelecionado != null)
{
aluno.setCursoAluno(cursoSelecionado.getIdCurso());
}
else
{
JOptionPane.showMessageDialog(null, "Erro: Nenhum curso selecionado.");
}
alunodao.Adicionar(aluno);
limparCampos();
JOptionPane.showMessageDialog(null, "Aluno cadastrado com sucesso.");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Erro ao cadastrar aluno: " + ex.getMessage());
}
}
private void comboCursoAncestorAdded(javax.swing.event.AncestorEvent evt) {
CursoDAO dao = new CursoDAO();
List<Curso> lista = dao.BuscarCurso();
comboCurso.removeAllItems();
for(Curso curso : lista)
{
comboCurso.addItem(curso);
}
}
private void comboTurmaAncestorAdded(javax.swing.event.AncestorEvent evt) {
TurmaDAO dao = new TurmaDAO();
List<Turma> lista = dao.BuscarTurma();
comboTurma.removeAllItems();
for(Turma turma : lista)
{
comboTurma.addItem(turma);
}
}
private void btnBuscarAlunoActionPerformed(java.awt.event.ActionEvent evt) {
String termo = txtPesquisarAluno.getText();
buscar(termo);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
limparCampos();
DefaultTableModel modelo = (DefaultTableModel)tblAluno.getModel();
modelo.setNumRows(0);
btnExcluir.setEnabled(false);
btnEditar.setEnabled(false);
btnAdicionar.setEnabled(true);
}
private void tblAlunoMouseClicked(java.awt.event.MouseEvent evt) {
setarCampos();
}
private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {
try
{
AlunoDAO alunodao = new AlunoDAO();
Aluno aluno = new Aluno();
aluno.setNomeAluno(txtNomeAluno.getText());
aluno.setEmailAluno(txtEmailAluno.getText());
aluno.setTelAluno(txtTelAluno.getText());
SimpleDateFormat sdfInput = new SimpleDateFormat("dd/MM/yyyy");
try
{
String dataNascimentoInput = txtDataNascimentoAluno.getText().trim();
if (!dataNascimentoInput.isEmpty())
{
Date utilDate = sdfInput.parse(dataNascimentoInput);
java.sql.Date sqlDateFormatted = new java.sql.Date(utilDate.getTime());
aluno.setDataNascimentoAluno(sqlDateFormatted);
}
else
{
System.err.println("Erro: Data de nascimento inválida.");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Erro: Formato de data inválido.");
}
aluno.setEnderecoAluno(txtEnderecoAluno.getText());
aluno.setRgAluno(txtRgAluno.getText());
aluno.setCpfAluno(txtCpfAluno.getText());
aluno.setSexoAluno((String) comboSexo.getSelectedItem());
Turma turmaSelecionada = (Turma) comboTurma.getSelectedItem();
if (turmaSelecionada != null)
{
aluno.setTurmaAluno(turmaSelecionada.getIdTurma());
}
else
{
JOptionPane.showMessageDialog(null, "Erro: Nenhuma turma selecionada.");
}
Curso cursoSelecionado = (Curso) comboCurso.getSelectedItem();
if (cursoSelecionado != null)
{
aluno.setCursoAluno(cursoSelecionado.getIdCurso());
}
else
{
JOptionPane.showMessageDialog(null, "Erro: Nenhum curso selecionado.");
}
alunodao.Editar(aluno);
limparCampos();
JOptionPane.showMessageDialog(null, "Aluno cadastrado com sucesso.");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Erro ao cadastrar aluno: " + ex.getMessage());
}
}
public void limparCampos()
{
txtNomeAluno.setText("");
txtEmailAluno.setText("");
txtTelAluno.setText("");
txtDataNascimentoAluno.setText("");
txtEnderecoAluno.setText("");
txtRgAluno.setText("");
txtCpfAluno.setText("");
comboSexo.setSelectedIndex(-1);
comboTurma.setSelectedIndex(-1);
comboCurso.setSelectedIndex(-1);
}
private void setarCampos()
{
limparCampos();
int setar = tblAluno.getSelectedRow();
txtNomeAluno.setText(tblAluno.getModel().getValueAt(setar, 1).toString());
txtEmailAluno.setText(tblAluno.getModel().getValueAt(setar, 2).toString());
txtTelAluno.setText(tblAluno.getModel().getValueAt(setar, 3).toString());
txtEnderecoAluno.setText(tblAluno.getModel().getValueAt(setar, 4).toString());
String dataNaTabela = tblAluno.getModel().getValueAt(setar, 5).toString();
String dataFormatoBrasileiro = converterDataFormatoBrasileiro(dataNaTabela);
txtDataNascimentoAluno.setText(dataFormatoBrasileiro);
txtCpfAluno.setText(tblAluno.getModel().getValueAt(setar, 6).toString());
txtRgAluno.setText(tblAluno.getModel().getValueAt(setar, 7).toString());
String id = tblAluno.getValueAt(tblAluno.getSelectedRow(), 0).toString();
String nome = tblAluno.getValueAt(tblAluno.getSelectedRow(), 1).toString();
Object obj = new Object(id, nome);
comboTurma.setSelectedItem(obj);
}
private String converterDataFormatoBrasileiro(String dataFormatoAmericano) {
try
{
SimpleDateFormat formatoAmericano = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatoBrasileiro = new SimpleDateFormat("dd/MM/yyyy");
Date data = formatoAmericano.parse(dataFormatoAmericano);
return formatoBrasileiro.format(data);
}
catch (Exception e) {
e.printStackTrace();
return ""; // ou outra mensagem de erro, se preferir
}
}