public void entidadeInterface() {
tfCodigo.setText(String.valueOf(entidade.getIdCidade()));
tfNome.setText(entidade.getNome());
atualizaComboBox(); //Linha 250
cbEstado.setSelectedItem(entidade.getEstado().getNome() +" "+entidade.getEstado().getSigla());
}
public void atualizaComboBox() {
todosEstados=daoestado.find("order by nome"); //Linha 283
cbEstado.removeAllItems();
for (int i=0;i<todosEstados.size();i++){
Estado est=todosEstados.get(i);
cbEstado.addItem(est.getNome()+" "+est.getSigla());
}
}
Classe Inteira
package visao;
import dao.DaoCidade;
import dao.DaoEstado;
import estagioii.Cidade;
import estagioii.Estado;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class CadastroCidade1 extends javax.swing.JDialog implements ActionListener {
private Cidade entidade;
private DaoCidade dao;
private DaoEstado daoestado;
protected CadastroListener observador;
private ArrayList<Estado> todosEstados;
private CadastroCidade1(){
}
public CadastroCidade1(long id){
initComponents();
if (id==-1){
btnExcluir.setEnabled(false);
}
carregaEntidade(id);
entidadeInterface();
setModal(true);
setLocationRelativeTo(null);
pack();
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
pBotoes = new javax.swing.JPanel();
btnSalvar = new javax.swing.JButton();
btnSalvar.addActionListener(this);
btnAlterar = new javax.swing.JButton();
btnAlterar.addActionListener(this);
btnCancelar = new javax.swing.JButton();
btnCancelar.addActionListener(this);
btnExcluir = new javax.swing.JButton();
btnExcluir.addActionListener(this);
btnSair = new javax.swing.JButton();
btnSair.addActionListener(this);
pCampos = new javax.swing.JPanel();
tfCodigo = new javax.swing.JTextField();
tfNome = new javax.swing.JTextField();
cbEstado = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Cadastro de Cidades");
pBotoes.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
btnSalvar.setText("Salvar");
btnSalvar.setPreferredSize(new java.awt.Dimension(75, 27));
btnAlterar.setText("Alterar");
btnAlterar.setPreferredSize(new java.awt.Dimension(75, 27));
btnCancelar.setText("Cancelar");
btnCancelar.setMaximumSize(new java.awt.Dimension(80, 27));
btnCancelar.setMinimumSize(new java.awt.Dimension(80, 27));
btnCancelar.setPreferredSize(new java.awt.Dimension(75, 27));
btnExcluir.setText("Excluir");
btnExcluir.setPreferredSize(new java.awt.Dimension(75, 27));
btnSair.setText("Sair");
btnSair.setPreferredSize(new java.awt.Dimension(75, 27));
org.jdesktop.layout.GroupLayout pBotoesLayout = new org.jdesktop.layout.GroupLayout(pBotoes);
pBotoes.setLayout(pBotoesLayout);
pBotoesLayout.setHorizontalGroup(
pBotoesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(pBotoesLayout.createSequentialGroup()
.addContainerGap()
.add(btnSalvar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 86, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnAlterar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 86, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 86, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnExcluir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 86, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(82, 82, 82)
.add(btnSair, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 86, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pBotoesLayout.setVerticalGroup(
pBotoesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(pBotoesLayout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(pBotoesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(pBotoesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(btnSalvar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnAlterar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnExcluir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(org.jdesktop.layout.GroupLayout.BASELINE, btnSair, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(12, Short.MAX_VALUE))
);
jLabel1.setText("C\u00f3digo");
jLabel2.setText("Nome da Cidade");
jLabel3.setText("Estado");
org.jdesktop.layout.GroupLayout pCamposLayout = new org.jdesktop.layout.GroupLayout(pCampos);
pCampos.setLayout(pCamposLayout);
pCamposLayout.setHorizontalGroup(
pCamposLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(pCamposLayout.createSequentialGroup()
.addContainerGap()
.add(pCamposLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(tfCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel1))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(pCamposLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(tfNome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 369, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel2))
.add(15, 15, 15)
.add(pCamposLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3)
.add(cbEstado, 0, 105, Short.MAX_VALUE))
.addContainerGap())
);
pCamposLayout.setVerticalGroup(
pCamposLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(pCamposLayout.createSequentialGroup()
.addContainerGap()
.add(pCamposLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jLabel2)
.add(jLabel3))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(pCamposLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(tfCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(tfNome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cbEstado, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(pBotoes, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
.add(pCampos, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(pBotoes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(pCampos, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
/*Eventos dos botoes*/
public void actionPerformed(ActionEvent e) {
/*Eventos dos Bot�es */
if (e.getSource()==btnAlterar){
JOptionPane.showMessageDialog(null,"evento alterar nao implementado!");
}
else if (e.getSource()==btnSair){
dispose();
}
else if (e.getSource()==btnCancelar){
dispose();
}
else if (e.getSource()==btnExcluir){
String [] mensagens ={"Sim","N�o"};
if(verificaCampos()){
interfaceEntidade();
int confirm= JOptionPane.showOptionDialog(null,"Deseja realmente excluir esse registro","Aten��o",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,mensagens,null);
if (confirm==JOptionPane.YES_OPTION){
excluir();
if (observador!=null){
observador.mudancaPerformed();
}
dispose();
}
}
}
else if (e.getSource()==btnSalvar) {
if (btnExcluir.isEnabled()){
if (verificaCampos()){
interfaceEntidade();
alterar();
if (observador!=null){
observador.mudancaPerformed();
}
dispose();
}
}
else{
if (verificaCampos()){
interfaceEntidade();
inserir();
if (observador!=null){
observador.mudancaPerformed();
}
dispose();
}
}
}
}
public void inserir() {
dao.create(entidade);
}
public void alterar() {
dao.update(entidade);
}
public void excluir() {
dao.delete(entidade);
}
public void interfaceEntidade() {
entidade.setNome(tfNome.getText());
entidade.setEstado(todosEstados.get(cbEstado.getSelectedIndex()));
}
public void entidadeInterface() {
tfCodigo.setText(String.valueOf(entidade.getIdCidade()));
tfNome.setText(entidade.getNome());
atualizaComboBox();
cbEstado.setSelectedItem(entidade.getEstado().getNome() +" "+entidade.getEstado().getSigla());
}
public void carregaEntidade(long id) {
if (dao==null){
dao=new DaoCidade();
}
if (id==-1){
entidade=new Cidade();
}else{
entidade=dao.read(id);
}
}
public boolean verificaCampos() {
if (tfCodigo.getText().equals("")){
JOptionPane.showMessageDialog(null,"O campo C�DIGO deve ser preenchido!");
return false;
}
if (tfNome.getText().equals("")){
JOptionPane.showMessageDialog(null,"O campo NOME do estado deve ser preenchido!");
return false;
}
// if (tfSigla.getText().equals("")){
// JOptionPane.showMessageDialog(null,"O campo SIGLA do estado deve ser preenchido!");
// return false;
// }
else return true;
}
public void atualizaComboBox() {
todosEstados=daoestado.find("order by nome");
cbEstado.removeAllItems();
for (int i=0;i<todosEstados.size();i++){
Estado est=todosEstados.get(i);
cbEstado.addItem(est.getNome()+" "+est.getSigla());
}
}
public void setCadastroListener(CadastroListener observador){
this.observador=observador;
}
// Variables declaration - do not modify
protected javax.swing.JButton btnAlterar;
protected javax.swing.JButton btnCancelar;
protected javax.swing.JButton btnExcluir;
protected javax.swing.JButton btnSair;
protected javax.swing.JButton btnSalvar;
public javax.swing.JComboBox cbEstado;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
public javax.swing.JPanel pBotoes;
public javax.swing.JPanel pCampos;
public javax.swing.JTextField tfCodigo;
public javax.swing.JTextField tfNome;
// End of variables declaration
}
Valew