Bom dia galera do GUJ, sou novo e iniciante em java POO, estou implementando um trabalho interdiciplinar que consiste em criar um determinado software no meu caso esse software é direcionado para um sistema imobiliaria.
E minha duvida é o seguinte:
Como eu faço para carregar os dados buscados no banco de dados que serão exibidos em um JFrame BuscarImobiliaria e Carrega-los para outra JFrame denomida de CadastrarImobiliaria, eu ja consigo buscar as informações perfeitamente apenas preciso fazer a ligação das 2 telas para que atravez de um botão Carregar dados as informaçãos ou formulado seja transferida para a tela CadastroImobiliaria para que assim possa realizar as funções alterar e deletar.
Ahhh ja estava me esquecendo, eu quero selecionar os dados buscados atravez de um click.
eu estou utilizando o seguinte código para selecionar os dados.
setObjeto(lista.get(tabela.getSelectedRow()));
ele se acontra no evento tabelaMouseClicked
e apartir do momento que eu seleciono eu quero clicar no button carregar dados e transferir o formulario para a tela de Cadastro.
A Minha tela Buscar dado é a seguinte:
package Telas;
import imobil.Controle.ImobiliariaControle;
import imobil.Entidades.Imobiliaria;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class BuscarImobiliaria extends javax.swing.JFrame {
private ImobiliariaControle controle;
private List<Imobiliaria> lista;
public void montaTabela() {
lista = controle.montaLista(cFiltro.getSelectedIndex(), fFiltro.getText());
DefaultTableModel modelo = new DefaultTableModel();
modelo.addColumn("Código");
modelo.addColumn("Nome");
modelo.addColumn("Creci");
for (Iterator<Imobiliaria> it = lista.iterator(); it.hasNext();) {
Imobiliaria imo = it.next();
modelo.addRow(new Object[]{imo.getId(), imo.getNome_imobiliaria(),imo.getCreci_imobiliaria()});
}
tabela.setModel(modelo);
}
public BuscarImobiliaria() {
initComponents();
controle = ImobiliariaControle.getInstance();
montaTabela();
}
@SuppressWarnings("unchecked")
//codigo esta oculto
private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {
setObjeto(lista.get(tabela.getSelectedRow()));
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
JOptionPane.showMessageDialog(null, "Ops! Opção invalida...",
"Atenção", JOptionPane.WARNING_MESSAGE);
}
private void btFiltraActionPerformed(java.awt.event.ActionEvent evt) {
montaTabela();
}
private void btCarregarDadosActionPerformed(java.awt.event.ActionEvent evt) {
// Aqui eu quero passar as informações para o Formulario de cadastro
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BuscarImobiliaria().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btCarregarDados;
private javax.swing.JButton btFiltra;
private javax.swing.JComboBox cFiltro;
private javax.swing.JTextField fFiltro;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tabela;
// End of variables declaration
private void setObjeto(Imobiliaria get) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
E a minha tela Cadastro Imobiliaria que eh onde eu quero apresentar os dados para realizar a alteracao etc.
package Telas;
import java.util.List;
import javax.swing.JOptionPane;
import imobil.Controle.ImobiliariaControle;
import imobil.Entidades.Imobiliaria;
import javax.swing.JFrame;
public class CadImobiliaria extends javax.swing.JFrame {
private Imobiliaria objeto;
private ImobiliariaControle controle;
private List<Imobiliaria> lista;
public void botoes(String acao) {
if (acao.equals("selecionado")) {
fCelular.setEditable(false);
fCidade.setEditable(false);
fCnpj.setEditable(false);
fCreci.setEditable(false);
fEmail_1.setEditable(false);
fEmail_2.setEditable(false);
fEndereco.setEditable(false);
fFax.setEditable(false);
fId.setEditable(false);
fNome.setEditable(false);
fSite.setEditable(false);
fTelefone.setEditable(false);
cUf.setEnabled(false);
btNovo.setEnabled(true);
btAlterar.setEnabled(true);
btDeletar.setEnabled(true);
btSalvar.setEnabled(false);
btBuscar.setEnabled(false);
btCancelar.setEnabled(true);
btSair.setEnabled(true);
} else if (acao.equals("novo") || acao.equals("alterar")) {
fCelular.setEditable(true);
fCidade.setEditable(true);
fCnpj.setEditable(true);
fCreci.setEditable(true);
fEmail_1.setEditable(true);
fEmail_2.setEditable(true);
fEndereco.setEditable(true);
fFax.setEditable(true);
fId.setEditable(false);
fNome.setEditable(true);
fSite.setEditable(true);
fTelefone.setEditable(true);
cUf.setEnabled(true);
btNovo.setEnabled(true);
btAlterar.setEnabled(false);
btDeletar.setEnabled(false);
btSalvar.setEnabled(true);
btBuscar.setEnabled(false);
btCancelar.setEnabled(true);
btSair.setEnabled(false);
} else if (acao.equals("inicio") || acao.equals("cancelar") || acao.equals("salvar")) {
fCelular.setEditable(false);
fCidade.setEditable(false);
fCnpj.setEditable(false);
fCreci.setEditable(false);
fEmail_1.setEditable(false);
fEmail_2.setEditable(false);
fEndereco.setEditable(false);
fFax.setEditable(false);
fId.setEditable(false);
fNome.setEditable(false);
fSite.setEditable(false);
fTelefone.setEditable(false);
cUf.setEnabled(false);
btNovo.setEnabled(true);
btAlterar.setEnabled(false);
btDeletar.setEnabled(false);
btSalvar.setEnabled(false);
btBuscar.setEnabled(true);
btCancelar.setEnabled(false);
btSair.setEnabled(true);
}
}
public void limpaCampos(){
fCelular.setText("");
fCidade.setText("");
fCnpj.setText("");
fCreci.setText("");
fEmail_1.setText("");
fEmail_2.setText("");
fEndereco.setText("");
fFax.setText("");
fId.setText("");
fNome.setText("");
fSite.setText("");
fTelefone.setText("");
cUf.setSelectedItem(null);
}
public boolean verificaCampos() {
if (fNome.getText().equals("") || fNome.getText() == null) {
JOptionPane.showMessageDialog(null, "O campo NOME deve ser preenchido!",
"Atenção", JOptionPane.WARNING_MESSAGE);
fNome.requestFocus();
return false;
} else if (fCnpj.getText().equals(" . . / - ") || fCnpj.getText() == null) {
JOptionPane.showMessageDialog(null, "O campo CNPJ deve ser preenchido!",
"Atenção", JOptionPane.WARNING_MESSAGE);
fCnpj.requestFocus();
return false;
} else if (fCreci.getText().equals(" - ") || fCreci.getText() == null) {
JOptionPane.showMessageDialog(null, "O campo CRECI deve ser preenchido!",
"Atenção", JOptionPane.WARNING_MESSAGE);
fCreci.requestFocus();
return false;
} else if (fEndereco.getText().equals("") || fEndereco.getText() == null) {
JOptionPane.showMessageDialog(null, "O campo Endereço deve ser preenchido!",
"Atenção", JOptionPane.WARNING_MESSAGE);
fEndereco.requestFocus();
return false;
} else if (fCidade.getText().equals("") || fCidade.getText() == null) {
JOptionPane.showMessageDialog(null, "O campo cidade deve ser preenchido!",
"Atenção", JOptionPane.WARNING_MESSAGE);
fCidade.requestFocus();
return false;
} else if (cUf.getSelectedItem() == null) {
JOptionPane.showMessageDialog(null, "O campo estado deve ser preenchido!",
"Atenção", JOptionPane.WARNING_MESSAGE);
cUf.requestFocus();
return false;
} else {
return true;
}
}
public Imobiliaria getObjeto() {
return objeto;
}
public void setObjeto(Imobiliaria objeto) {
this.objeto = objeto;
}
public CadImobiliaria() {
initComponents();
controle = ImobiliariaControle.getInstance();
botoes("inicio");
}
@SuppressWarnings("unchecked")
// O codigo foi ocultado
private void btDeletarActionPerformed(java.awt.event.ActionEvent evt) {
// falta implementar a funcao deletar
}
private void btBuscarActionPerformed(java.awt.event.ActionEvent evt) {
botoes("selecionado");
BuscarImobiliaria bu = new BuscarImobiliaria();
bu.setLocationRelativeTo(null);
bu.setVisible(true);
// acredito que os dados da tabela buscar devao ser retornados aqui.
}
private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {
if (verificaCampos() == true){
objeto = new Imobiliaria();
objeto.setCelular_imobiliaria(fCelular.getText());
objeto.setCidade_imobiliaria(fCidade.getText());
objeto.setCnpj_imobiliaria(fCnpj.getText());
objeto.setCreci_imobiliaria(fCreci.getText());
objeto.setEmail1_imobiliaria(fEmail_1.getText());
objeto.setEmail2_imobiliaria(fEmail_2.getText());
objeto.setEndereco_imobiliaria(fEndereco.getText());
objeto.setEstado_imobiliaria(cUf.getSelectedItem().toString());
objeto.setFax_imobiliaria(fFax.getText());
objeto.setNome_imobiliaria(fNome.getText());
objeto.setSite_imobiliaria(fSite.getText());
objeto.setTelefone_imobiliaria(fTelefone.getText());
if (fId.getText().equals("")){
objeto.setId(null);
} else {
objeto.setId(Long.parseLong(fId.getText()));
}
controle.salvar(objeto);
botoes("salvar");
}
}
private void btSairActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {
botoes("novo");
limpaCampos();
fNome.requestFocus();
}
private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {
botoes("cancelar");
limpaCampos();
}
private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {
//falta implementar
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
JOptionPane.showMessageDialog(null, "Ops! Opção invalida...",
"Atenção", JOptionPane.WARNING_MESSAGE);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CadImobiliaria().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btAlterar;
private javax.swing.JButton btBuscar;
private javax.swing.JToggleButton btCancelar;
private javax.swing.JButton btDeletar;
private javax.swing.JButton btNovo;
private javax.swing.JButton btSair;
private javax.swing.JButton btSalvar;
private javax.swing.JComboBox cUf;
private javax.swing.JFormattedTextField fCelular;
private javax.swing.JTextField fCidade;
private javax.swing.JFormattedTextField fCnpj;
private javax.swing.JFormattedTextField fCreci;
private javax.swing.JTextField fEmail_1;
private javax.swing.JTextField fEmail_2;
private javax.swing.JTextField fEndereco;
private javax.swing.JFormattedTextField fFax;
private javax.swing.JTextField fId;
private javax.swing.JTextField fNome;
private javax.swing.JTextField fSite;
private javax.swing.JFormattedTextField fTelefone;
private javax.swing.JComboBox jComboBox3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JLabel lNomeImobi;
private javax.swing.JLabel lcnpjImobi;
// End of variables declaration
}
Caso necessitem postarei a Classe com as Entidades, getters and setters, construtores hasch Code.
Espero que entendao e me ajudem!!
Obrigado.