boa tarde.
Estou tentando preencher um jtable com o retorno de uma list do hibernate.
Codigo da tela de cadastro
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.TabExpander;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTable;
import javax.swing.JButton;
import org.hibernate.cfg.annotations.reflection.XMLContext.Default;
public class Setor extends JDialog {
/**
*
*/
private static final long serialVersionUID = 5518758177627322408L;
private MenuBotoes mnuBotoes = new MenuBotoes();
private JPanel pnlPrincipal = new JPanel();
private JTextField txtCodigo;
private JTextField txtDescricao;
private JTextField txtLimite;
private JTextField txtCodCCusto;
private JTextField txtCCusto;
private JButton btnCodigo;
private JButton btnDescricao;
private JTable tblGrid;
private Conexao conexao;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Setor dialog = new Setor();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public Setor() {
setModal(true);
setBounds(100, 100, 450, 355);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
pnlPrincipal.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(mnuBotoes.getPanel(), BorderLayout.NORTH);
pnlPrincipal.setLayout(new MigLayout("", "[33px,grow][20px][][][][][][grow][][][][grow][33px][33px][33px][33px][33px][][33px][33px][33px][33px][33px][33px][33px][][33px]", "[20px][][][][grow][grow][grow][][][][][]"));
getContentPane().add(pnlPrincipal, BorderLayout.CENTER);
JLabel lblCodigo = new JLabel("Codigo");
pnlPrincipal.add(lblCodigo, "cell 0 0,alignx left,aligny center");
JLabel lblDescrio = new JLabel("Descri\u00E7\u00E3o");
pnlPrincipal.add(lblDescrio, "cell 3 0");
txtCodigo = new JTextField();
pnlPrincipal.add(txtCodigo, "cell 0 1 2 1,alignx left,aligny top");
txtCodigo.setColumns(5);
txtDescricao = new JTextField();
pnlPrincipal.add(txtDescricao, "flowx,cell 3 1 24 1,growx");
txtDescricao.setColumns(10);
JLabel lblLimite = new JLabel("Limite");
pnlPrincipal.add(lblLimite, "cell 0 2");
JLabel lblCentroDeCusto = new JLabel("Centro de Custo");
pnlPrincipal.add(lblCentroDeCusto, "cell 7 2");
txtLimite = new JTextField();
pnlPrincipal.add(txtLimite, "cell 0 3 6 1,growx");
txtLimite.setColumns(10);
txtCodCCusto = new JTextField();
pnlPrincipal.add(txtCodCCusto, "cell 7 3,growx");
txtCodCCusto.setColumns(10);
txtCCusto = new JTextField();
pnlPrincipal.add(txtCCusto, "cell 8 3 19 1,growx");
txtCCusto.setColumns(10);
btnCodigo = new JButton("?");
pnlPrincipal.add(btnCodigo, "cell 2 1");
btnDescricao = new JButton("?");
pnlPrincipal.add(btnDescricao, "cell 3 1 24 1");
mnuBotoes.controlaBotoes(1);
liberaCampos(1);
tblGrid = new JTable();
final DefaultTableModel modelo= (DefaultTableModel)tblGrid.getModel();
modelo.setNumRows(0);
pnlPrincipal.add(tblGrid, "cell 0 5 27 7,grow");
btnCodigo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
conexao = new Conexao();
List<XeroxSetores> setor = conexao.selectList(txtCodigo.getText().toString(), 1);
int tamanhoLista = setor.size();
for (int i = 0; i <= tamanhoLista - 1; i++){
modelo.addRow(new Object [] {setor.get(i).getCodSetor().toString(),setor.get(i).getDescricao().toString(),
setor.get(i).getLimite(),setor.get(i).getCodCCusto().toString()});
tblGrid.setModel(modelo);
}
} catch (Exception e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Erro ao Pesquisar: " + e1.getMessage());
}
}
});
mnuBotoes.getbtNovo().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mnuBotoes.controlaBotoes(3);
txtDescricao.setFocusable(true);
limpaCampos();
liberaCampos(2);
}
});
mnuBotoes.getbtAlterar().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mnuBotoes.controlaBotoes(3);
txtDescricao.setFocusable(true);
limpaCampos();
liberaCampos(3);
}
});
mnuBotoes.getbtExcluir().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (txtCodigo.getText()==""){
JOptionPane.showMessageDialog(null, "Campo Código não pode ficar vazio");
txtCodigo.setFocusable(true);
}
else{
try {
conexao = new Conexao();
XeroxSetores setor = conexao.select(txtCodigo.getText().toString());
if (setor != null){
conexao.delete(txtCodigo.getText().toString());
JOptionPane.showMessageDialog(null, "Excluido com Sucesso");
mnuBotoes.controlaBotoes(2);
limpaCampos();
liberaCampos(1);
}
else{
JOptionPane.showMessageDialog(null, "Setor não Encontrado");
txtCodigo.setFocusable(true);
}
mnuBotoes.controlaBotoes(2);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Erro ao Excluir: " + e.getMessage());
}
}
}
});
mnuBotoes.getbtGravar().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (txtCodigo.getText()==""){
JOptionPane.showMessageDialog(null, "Campo Código não pode ficar vazio");
txtCodigo.setFocusable(true);
}
else if (txtDescricao.getText()==""){
JOptionPane.showMessageDialog(null, "Campo Descrição não pode ficar vazio");
txtDescricao.setFocusable(true);
}
else if (txtLimite.getText()==""){
JOptionPane.showMessageDialog(null, "Campo Limite não pode ficar vazio");
txtLimite.setFocusable(true);
}
else{
XeroxSetores setor = new XeroxSetores();
setor.setCodSetor(txtCodigo.getText().toString());
setor.setDescricao(txtDescricao.getText().toString());
setor.setLimite(Integer.parseInt(txtLimite.getText().toString()));
setor.setCodCCusto(txtCodCCusto.getText().toString());
try {
conexao = new Conexao();
conexao.insert(setor);
JOptionPane.showMessageDialog(null, "Cadastrado com Sucesso");
limpaCampos();
liberaCampos(1);
mnuBotoes.controlaBotoes(2);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Erro ao Cadastrar: " + e.getMessage());
}
}
}
});
mnuBotoes.getbtCancelar().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mnuBotoes.controlaBotoes(2);
limpaCampos();
liberaCampos(1);
}
});
}
public void liberaCampos(int pIndex){
if (pIndex == 1) {
//Pesquisa
txtCodigo.setEnabled(true);
txtDescricao.setEnabled(true);
txtLimite.setEnabled(false);
txtCodCCusto.setEnabled(false);
txtCCusto.setEnabled(false);
}
else if (pIndex == 2) {
//Gravar insert
txtCodigo.setEnabled(true);
txtDescricao.setEnabled(true);
txtLimite.setEnabled(true);
txtCodCCusto.setEnabled(true);
txtCCusto.setEnabled(true);
}
else if (pIndex == 3) {
//Gravar update
txtCodigo.setEnabled(false);
txtDescricao.setEnabled(true);
txtLimite.setEnabled(true);
txtCodCCusto.setEnabled(true);
txtCCusto.setEnabled(true);
}
}
public void limpaCampos() {
txtCodigo.setText("");
txtDescricao.setText("");
txtLimite.setText("");
txtCodCCusto.setText("");
txtCCusto.setText("");
}
}
Codigo do arquivo de conexão
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Conexao {
//private SessionFactory factory;
private EntityManager em;
private EntityManagerFactory emf;
public Conexao() throws Exception {
emf = Persistence.createEntityManagerFactory("xeroxPU");
em = emf.createEntityManager();
}
public void insert(XeroxSetores setor) throws Exception {
em.getTransaction().begin();
em.persist(setor);
em.getTransaction().commit();
em.close();
emf.close();
}
public List<XeroxSetores> selectList(String condicao, int tipo) {
return em.createQuery("SELECT CodSetor, Descricao, Limite, CodCCusto FROM XeroxSetores").getResultList();
}
public XeroxSetores select(String pk) throws Exception {
XeroxSetores setor = em.find(XeroxSetores.class, pk);
return setor;
}
public void update(XeroxSetores setor, String pk) throws Exception {
XeroxSetores setorantigo = em.find(XeroxSetores.class, pk);
setorantigo = setor;
em.getTransaction().begin();
em.merge(setorantigo);
em.getTransaction().commit();
em.close();
emf.close();
}
public void delete(String pk) throws Exception {
XeroxSetores setor = em.find(XeroxSetores.class, pk);
em.getTransaction().begin();
em.remove(setor);
em.getTransaction().commit();
em.close();
emf.close();
}
}
Minha ideia é que quando eu clicar no botão btnCodigo, preencha o jtable.
Obs: Insert e Delete está funcionando.
Gostaria de saber também, como eu faço para criar um TableModel personalizado e utiliza-lo em meus códigos.
Estou no aguardo!
Obrigado