Ao fazer uma nova entra no banco de dados através da interface criada no netbeans, é preciso fechar a aplicação e abri-la para que a nova entrada tenha efeito e apareça na listagem.
Exemplo, eu faço o cadastro de um novo item no campo grupo, “05 - Teste”, para que ele paree conforme o cadastrado e não “dto.Grupo[id=0]” que é a referencia do campo no banco de dados, eu preciso fechar a aplicação e assim depois ela aparece como o cadastrado.
Na idéia é que toda vez que é fechada a conexão com o banco de dados e feita uma nova a atualização e é feita, e não em “tempo real” como deveria ser.
Segue a imagem.

posta ai o codigo de salvar…e a parte do seu combo? como vc add o objeto na lista…
Código da camada DAO:
[code]package dao;
public class GrupoDAO {
private EntityManagerFactory emf;
public GrupoDAO(EntityManagerFactory emf) {
this.emf = emf;
}
public boolean save(Grupo grupo) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
em.merge(grupo);
em.getTransaction().commit();
return true;
} catch (Exception e) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} finally {
em.close();
}
return false;
}
public boolean delete(int id) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
Grupo grupo = em.find(Grupo.class, id);
if (grupo != null) {
em.remove(grupo);
em.getTransaction().commit();
return true;
}
} catch (Exception e) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} finally {
em.close();
}
return false;
}
}[/code]
O problema também ocorre quando eu faço um novo cadastro e tento fazer a busca, ele diz que não existe, mas se eu fecha a aplicação e abro novamente, ela é listada normalmente.
Muito obrigado,
aparentemente ta tudo ok…no seu DAO…cade o codigo do combox?posta ai…
então, no combo eu uso um código da biblioteca de um amigo meu, e talz, ae eu não sei o código exato por tras desse “aplicaRenderer”,
humm…talvez seja isso…
faz um teste assim
private Vector opcoes = new Vector();
opcoes.add(seuObjeto.getDescricao());
jComboBox1 = new JComboBox(opcoes);
jComboBox1.updateUI();
testa ai…v se dar certo…
aproveita e roda esse exemplo no netbeans mesmo…
/*
* Principal.java
*
* Created on 5 de Dezembro de 2008, 16:39
*/
package tela;
import java.util.Vector;
import javax.swing.JComboBox;
/**
*
* @author Abel Gomes
*/
public class Principal extends javax.swing.JFrame {
/** Creates new form Principal */
public Principal() {
initComponents();
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox();
jComboBox1 = new JComboBox(opcoes);
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Tela de Testes");
jLabel1.setText("Texto para o Combo");
jLabel2.setText("Itens");
jButton1.setText("Add");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Remover");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addGap(70, 70, 70)
.addComponent(jButton2)
.addGap(85, 85, 85))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(87, 87, 87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(!jTextField1.getText().equals("")){
opcoes.add(jTextField1.getText());
jTextField1.setText("");
jComboBox1.updateUI();
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
opcoes.remove(jComboBox1.getSelectedItem());
jComboBox1.setSelectedIndex(-1);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
private Vector opcoes = new Vector();
}
funcioanl leal seu código, vo estuda ele e adapta pra aplicação, mais eu tenho que penar como isso tudo funciona de acordo com o banco de dados, rs
entao…seu problema é que vc esta add um objeto…no combo…V se vc nao tem como pegar esse codigo dele…seria bem interessante dar uma olhada nele…
se vc nao conseguir sobrescreve o toString…e nele vc retorna cod +" – "+ descricao;
entao no combo aparece
1 – teste
qaundo ocara clicar vc pega o 1 sacou…