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.CaixaController; 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.Caixa; 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 CaixasView extends javax.swing.JInternalFrame { private DefaultTableModel model; private CaixaController controller; private List<Caixa> data; private DateUtil dateUtil; private DoubleUtil doubleUtil; /** * Creates new form UsuariosView */ public CaixasView() { try { controller = CaixaController.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.findAll(); model = (DefaultTableModel) jTable.getModel(); model.getDataVector().removeAllElements(); model.fireTableDataChanged(); data.forEach(item -> add(item)); model.fireTableDataChanged(); tableEvent(); } private void tableEvent() { if (jTable.getSelectedRowCount() > 0) { jButtonAlterar.setEnabled(true); jButtonExcluir.setEnabled(true); } else { jButtonAlterar.setEnabled(false); jButtonExcluir.setEnabled(false); } } private void add(Caixa caixa) { Object[] vetor = new Object[5]; vetor[0] = caixa.getId(); vetor[1] = caixa.getDescricao(); vetor[2] = dateUtil.format(caixa.getData()); vetor[3] = caixa.getCreditoDebito(); vetor[4] = doubleUtil.format(caixa.getValor()); model.addRow(vetor); } private void openModal() { CaixaView caixaView = new CaixaView(null, true); caixaView.setCaixa(getSelecionado()); caixaView.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 Caixa getSelecionado() { if (jTable.getSelectedRowCount() > 0) { Object value = model.getValueAt(jTable.getSelectedRow(), 0); return controller.find(Long.parseLong(value.toString())); } return null; } private void pesquisa() { String query = pesquisa.getText(); model = (DefaultTableModel) jTable.getModel(); model.getDataVector().removeAllElements(); model.fireTableDataChanged(); data.stream().filter(e -> StringUtils.containsIgnoreCase(e.getId().toString(), 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(); jPanel3 = new javax.swing.JPanel(); pesquisa = new javax.swing.JTextField(); setClosable(true); setIconifiable(true); setMaximizable(true); setTitle("Lanamentos"); 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", "Data", "Credito / Dbito", "Valor" }) { boolean[] canEdit = new boolean[] { 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); 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() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 897, Short.MAX_VALUE) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE) .addContainerGap())); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisa")); pesquisa.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { pesquisaKeyReleased(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) .addContainerGap(607, Short.MAX_VALUE))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap() .addComponent(pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .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 // 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.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.JTextField pesquisa; // End of variables declaration//GEN-END:variables }