Estou fazendo uma tabela de requisição de produto, mas estou encontrando dificuldades em adicionar os produtos da requisição selecionando os produtos que estão em uma outra lista.
[code]
public class RequisicaoForm extends javax.swing.JFrame {
private Requisicao req;
private List departamentos;
/** Creates new form RequisicaoForm */
public RequisicaoForm() {
initComponents();
carregaDepartamento();
}
/////////table produto
private class ProdutoTableModel extends DefaultTableModel {
private List<Produto> produto;
public ProdutoTableModel() {
String[] titulos = {"Codigo", "Nome", "Preco de Venda", "Preco com Imposto","Preco sem Imposto",
"Saldo Atual","Qtd Maxima","Qtd Minima","Codigo Tipo","Codigo Grupo"
,"Codigo Fornecedor"};
this.setColumnIdentifiers(titulos);
produto = new ArrayList<Produto>();
}
public Produto getProdutoPeloIndice(int index) {
return produto.get(index);
}
public void adicionarProduto(Produto produto) {
this.addRow(new Object[]{produto.getCod_produto(), produto.getProduto(), produto.getPreco_venda()
, produto.getCusto_com_imposto(), produto.getCusto_sem_imposto(), produto.getSaldo(),
produto.getQtd_maxima(), produto.getQtd_minima(), produto.getCod_tipo(),
produto.getCod_grupo(), produto.getCod_fornecedor()});
}
public void removeAll() {
int size;
while ( (size = getRowCount()) > 0 ) {
removeRow(size-1);
}
}
public void setProduto(List<Produto> produto) {
this.produto = produto;
removeAll();
for ( Produto b : produto ) {
adicionarProduto(b);
}
}
public void setLinha(List<Produto> produto) {
this.produto = produto;
removeAll();
for ( Produto b : produto ) {
adicionarProduto(b);
}
}
}
//Carrega Box Departamento***************************
private void carregaDepartamento() {
try {
departamentos = (new RequisicaoDAO()).codDepartamento();
for (Departamento d : departamentos) {
deptoBox.addItem(d);
}
} catch (EstoqueException ex) {
JOptionPane.showMessageDialog(null, "Erro ao carregar dep");
ex.printStackTrace();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Erro ao carregar dep");
ex.printStackTrace();
}
}
/*
public void updatetxtProduto(Produto produto) {
this.produto = produto;
txtRequisicao2.setText(String.valueOf(produto.getCodigo()));
}*/
/** 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() {
PanelReq = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
deptoBox = new javax.swing.JComboBox();
txtCod = new javax.swing.JTextField();
tipoBox = new javax.swing.JComboBox();
txtData = new javax.swing.JFormattedTextField();
PanelItemReq = new javax.swing.JPanel();
btProduto = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTableReq = new javax.swing.JTable();
txtTotal = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
jTableEscolha = new javax.swing.JTable();
jLabel6 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
btMostraProdutos = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
btSair = new javax.swing.JButton();
btExcluir = new javax.swing.JButton();
btLimpar = new javax.swing.JButton();
btSalvar = new javax.swing.JButton();
btNovo = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Cadastro Requisição");
PanelReq.setEnabled(false);
jLabel1.setText("Código");
jLabel2.setText("Data da Requisição");
jLabel3.setText("Tipo");
jLabel4.setText("Departamento");
txtCod.setEnabled(false);
tipoBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Entrada", "Saída" }));
tipoBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tipoBoxActionPerformed(evt);
}
});
txtData.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtDataActionPerformed(evt);
}
});
javax.swing.GroupLayout PanelReqLayout = new javax.swing.GroupLayout(PanelReq);
PanelReq.setLayout(PanelReqLayout);
PanelReqLayout.setHorizontalGroup(
PanelReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PanelReqLayout.createSequentialGroup()
.addGroup(PanelReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PanelReqLayout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(PanelReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel3)))
.addGroup(PanelReqLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(PanelReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(PanelReqLayout.createSequentialGroup()
.addComponent(txtCod, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(tipoBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(deptoBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(183, Short.MAX_VALUE))
);
PanelReqLayout.setVerticalGroup(
PanelReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PanelReqLayout.createSequentialGroup()
.addContainerGap()
.addGroup(PanelReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(PanelReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tipoBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(PanelReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(deptoBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
PanelItemReq.setEnabled(false);
btProduto.setText("Escolher Produto");
btProduto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btProdutoActionPerformed(evt);
}
});
jTableReq.setModel(TableModel2);
jTableReq.setEnabled(false);
jScrollPane1.setViewportView(jTableReq);
txtTotal.setEnabled(false);
txtTotal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtTotalActionPerformed(evt);
}
});
jLabel5.setText("Total");
jTableEscolha.setModel(TableModel);
jScrollPane2.setViewportView(jTableEscolha);
jLabel6.setText("Escolha o produto:");
jButton1.setText("Adicionar Produto");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
btMostraProdutos.setText("Listar Produto");
btMostraProdutos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btMostraProdutosActionPerformed(evt);
}
});
javax.swing.GroupLayout PanelItemReqLayout = new javax.swing.GroupLayout(PanelItemReq);
PanelItemReq.setLayout(PanelItemReqLayout);
PanelItemReqLayout.setHorizontalGroup(
PanelItemReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PanelItemReqLayout.createSequentialGroup()
.addContainerGap()
.addGroup(PanelItemReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PanelItemReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PanelItemReqLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 607, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(PanelItemReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PanelItemReqLayout.createSequentialGroup()
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(btMostraProdutos)
.addContainerGap(401, Short.MAX_VALUE))
.addGroup(PanelItemReqLayout.createSequentialGroup()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(btProduto)
.addContainerGap(358, Short.MAX_VALUE))
.addGroup(PanelItemReqLayout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
.addContainerGap())))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelItemReqLayout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(218, 218, 218))))
);
PanelItemReqLayout.setVerticalGroup(
PanelItemReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PanelItemReqLayout.createSequentialGroup()
.addContainerGap()
.addGroup(PanelItemReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel6)
.addComponent(btMostraProdutos))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(PanelItemReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(btProduto))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(PanelItemReqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addContainerGap())
);
btSair.setText("Sair");
btSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSairActionPerformed(evt);
}
});
btExcluir.setText("Excluir");
btExcluir.setEnabled(false);
btLimpar.setText("Limpar");
btLimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btLimparActionPerformed(evt);
}
});
btSalvar.setText("Salvar");
btSalvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSalvarActionPerformed(evt);
}
});
btNovo.setText("Novo");
btNovo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btNovoActionPerformed(evt);
}
});
private void btSairActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {
//limpar();
}
private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (req == null) {
req = new Requisicao();
}
/*Seta os dados informados na tela para o objeto que será passado como parâmetro para inserir ou alterar*/
try {
//java.text.SimpleDateFormat dtfGeral = new java.text.SimpleDateFormat("ddMMyyyy");
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
try {
req.setData_requisicao((Date) df.parse(txtData.getText()));
} catch (ParseException ex) {
Logger.getLogger(RequisicaoForm.class.getName()).log(Level.SEVERE, null, ex);
}
//req.setData_requisicao()
RequisicaoDAO dao = new RequisicaoDAO();
try {
dao.inserir(req);
//Limpa o formulário após salvar fornecedor
//limpar();
} catch (SQLException ex) {
Logger.getLogger(ProdutoForm.class.getName()).log(Level.SEVERE, null, ex);
}
//Limpa o formulário após salvar fornecedor
//limpar();
} catch (EstoqueException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
//TXTNome.setEnabled(false);
}
private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
txtCod.setText(new RequisicaoDAO().quantidadeReq().toString());
} catch (EstoqueException ex) {
Logger.getLogger(TipoForm.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(TipoForm.class.getName()).log(Level.SEVERE, null, ex);
}
PanelReq.setEnabled(rootPaneCheckingEnabled);
PanelItemReq.setEnabled(rootPaneCheckingEnabled);
//txtFantasia.setEnabled(rootPaneCheckingEnabled);
}
private void tipoBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtDataActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btProdutoActionPerformed(java.awt.event.ActionEvent evt) {
(new EscolheProdutoForm()).setVisible(true);
}
private void btMostraProdutosActionPerformed(java.awt.event.ActionEvent evt) {
List<Produto> produto = (new ProdutoDAO()).listarTodosOsProdutos();
TableModel.setProduto(produto);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//jTableReq.setv(jTableEscolha.getSelectedRow());
//System.out.println(jTableEscolha.getModel());
//jTableReq.get().setValueAt(PanelReq, WIDTH, WIDTH);
//jTableReq.setModel((TableModel) TableModel.getProdutoPeloIndice(jTableEscolha.getSelectedRow()));
//jTableEscolha.setValueAt(jTableReq.getModel());
}
private void txtTotalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private ProdutoTableModel TableModel = new ProdutoTableModel();
private ProdutoTableModel TableModel2 = new ProdutoTableModel();
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RequisicaoForm().setVisible(true);
}
});
}[/code]