Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package br.com.utfpr.pb.view; import br.com.utfpr.pb.controller.CategoriaController; import br.com.utfpr.pb.controller.ContaController; import br.com.utfpr.pb.controller.UsuarioController; import br.com.utfpr.pb.enumeration.TipoConta; import br.com.utfpr.pb.model.Categoria; import br.com.utfpr.pb.model.Conta; import br.com.utfpr.pb.model.Usuario; import br.com.utfpr.pb.util.DateUtil; import br.com.utfpr.pb.util.DoubleUtil; import org.apache.commons.lang.StringUtils; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.util.List; /** * @author Joo */ public class ContaPagarView extends javax.swing.JInternalFrame { private DefaultTableModel model; private ContaController controller; private List<Conta> data; private DateUtil dateUtil; private DoubleUtil doubleUtil; /** * Creates new form UsuariosView */ public ContaPagarView() { try { controller = ContaController.getInstance(); dateUtil = DateUtil.getInstance(); doubleUtil = DoubleUtil.getInstance(); initComponents(); initTable(); pesquisa(); Dimension desktopSize = PrincipalView.jDesktopPane.getSize(); Dimension jInternalFrameSize = getSize(); setLocation((desktopSize.width - jInternalFrameSize.width) / 2, (desktopSize.height - jInternalFrameSize.height) / 2); PrincipalView.jDesktopPane.add(this); setMaximum(true); setVisible(true); moveToFront(); setSelected(true); } catch (Exception e) { e.printStackTrace(); } } private void initTable() { data = controller.findByTipoConta(TipoConta.PAGAR); model = (DefaultTableModel) jTable.getModel(); model.getDataVector().removeAllElements(); model.fireTableDataChanged(); data.forEach(item -> add(item)); model.fireTableDataChanged(); totalSaldo.setText(doubleUtil.format(data.stream().mapToDouble(Conta::getSaldo).sum())); tableEvent(); } private void tableEvent() { if (jTable.getSelectedRowCount() > 0) { jButtonAlterar.setEnabled(true); jButtonExcluir.setEnabled(true); } else { jButtonAlterar.setEnabled(false); jButtonExcluir.setEnabled(false); } } private void add(Conta conta) { Object[] vetor = new Object[6]; vetor[0] = conta.getId(); vetor[1] = conta.getDescricao(); vetor[2] = conta.getPessoa().getNome(); vetor[3] = conta.getVencimento() != null ? dateUtil.format(conta.getVencimento()) : null; vetor[4] = doubleUtil.format(conta.getValor()); vetor[5] = doubleUtil.format(conta.getSaldo()); model.addRow(vetor); } private void openModal() { ContaView contaView = new ContaView(null, true); contaView.setConta(getSelecionado()); contaView.setVisible(true); initTable(); } private void excluir() { if (JOptionPane.showConfirmDialog(this, "Tem certeza que deseja excluir?") == 0) { try { controller.remove(getSelecionado()); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Ocorreu um problema ao excluir.\n\n" + e.getMessage()); } initTable(); } } private Conta getSelecionado() { if (jTable.getSelectedRowCount() > 0) { Object value = model.getValueAt(jTable.getSelectedRow(), 0); return controller.find(Long.parseLong(value.toString())); } Conta conta = new Conta(); conta.setTipoConta(TipoConta.PAGAR); return conta; } private void pesquisa() { String query = pesquisa.getText(); model = (DefaultTableModel) jTable.getModel(); model.getDataVector().removeAllElements(); model.fireTableDataChanged(); data.stream().filter(e -> naoQuitadas.isSelected() ? e.getSaldo() > 0 : true) .filter(e -> StringUtils.containsIgnoreCase(e.getId().toString(), query) || StringUtils.containsIgnoreCase(e.getPessoa().getNome(), query) || StringUtils.containsIgnoreCase(e.getDescricao(), query)) .forEach(item -> add(item)); model.fireTableDataChanged(); } /** * 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">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jButtonFechar = new javax.swing.JButton(); jButtonAlterar = new javax.swing.JButton(); jButtonExcluir = new javax.swing.JButton(); jButtonNovo = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); jTable = new javax.swing.JTable(); totalSaldo = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); pesquisa = new javax.swing.JTextField(); naoQuitadas = new javax.swing.JCheckBox(); setClosable(true); setIconifiable(true); setMaximizable(true); setTitle("Contas a Pagar"); jButtonFechar.setText("Fechar"); jButtonFechar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonFecharActionPerformed(evt); } }); jButtonAlterar.setText("Alterar"); jButtonAlterar.setEnabled(false); jButtonAlterar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonAlterarActionPerformed(evt); } }); jButtonExcluir.setText("Excluir"); jButtonExcluir.setEnabled(false); jButtonExcluir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonExcluirActionPerformed(evt); } }); jButtonNovo.setText("Novo"); jButtonNovo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonNovoActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jButtonNovo) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonAlterar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonExcluir) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonFechar).addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonFechar).addComponent(jButtonAlterar) .addComponent(jButtonExcluir).addComponent(jButtonNovo)) .addContainerGap())); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Listagem")); jTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Id", "Descrio", "Pessoa", "Vencimento", "Valor", "Saldo" }) { boolean[] canEdit = new boolean[] { false, false, false, false, false, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jTable.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTableMouseClicked(evt); } }); jTable.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { jTableKeyReleased(evt); } }); jScrollPane1.setViewportView(jTable); totalSaldo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N totalSaldo.setText("0,00"); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel2.setText("Total em aberto"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 897, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(totalSaldo))) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(totalSaldo).addComponent(jLabel2)))); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisa")); pesquisa.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { pesquisaKeyReleased(evt); } }); naoQuitadas.setSelected(true); naoQuitadas.setText("Somente em aberto"); naoQuitadas.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { naoQuitadasItemStateChanged(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout .setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap() .addComponent(pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(naoQuitadas).addContainerGap())); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(naoQuitadas)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableMouseClicked // TODO add your handling code here: tableEvent(); if (evt.getClickCount() == 2 && !evt.isConsumed()) { evt.consume(); //handle double click event. openModal(); } }//GEN-LAST:event_jTableMouseClicked private void jTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableKeyReleased // TODO add your handling code here: tableEvent(); }//GEN-LAST:event_jTableKeyReleased private void jButtonNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNovoActionPerformed // TODO add your handling code here: openModal(); }//GEN-LAST:event_jButtonNovoActionPerformed private void jButtonAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAlterarActionPerformed // TODO add your handling code here: openModal(); }//GEN-LAST:event_jButtonAlterarActionPerformed private void jButtonExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExcluirActionPerformed // TODO add your handling code here: excluir(); }//GEN-LAST:event_jButtonExcluirActionPerformed private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed // TODO add your handling code here: dispose(); }//GEN-LAST:event_jButtonFecharActionPerformed private void pesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pesquisaKeyReleased // TODO add your handling code here: pesquisa(); }//GEN-LAST:event_pesquisaKeyReleased private void naoQuitadasItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_naoQuitadasItemStateChanged // TODO add your handling code here: pesquisa(); }//GEN-LAST:event_naoQuitadasItemStateChanged // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonAlterar; private javax.swing.JButton jButtonExcluir; private javax.swing.JButton jButtonFechar; private javax.swing.JButton jButtonNovo; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable; private javax.swing.JCheckBox naoQuitadas; private javax.swing.JTextField pesquisa; private javax.swing.JLabel totalSaldo; // End of variables declaration//GEN-END:variables }