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.ClienteController; import br.com.utfpr.pb.controller.UsuarioController; import br.com.utfpr.pb.model.Cliente; import br.com.utfpr.pb.model.Usuario; 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 ClientesView extends javax.swing.JInternalFrame { private DefaultTableModel model; private ClienteController controller; private List<Cliente> data; /** * Creates new form UsuariosView */ public ClientesView() { try { controller = ClienteController.getInstance(); initComponents(); initTable(); Dimension desktopSize = PrincipalView.jDesktopPane.getSize(); Dimension jInternalFrameSize = getSize(); setLocation((desktopSize.width - jInternalFrameSize.width) / 2, (desktopSize.height - jInternalFrameSize.height) / 2); PrincipalView.jDesktopPane.add(this); 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(Cliente cliente) { Object[] vetor = new Object[5]; vetor[0] = cliente.getId(); vetor[1] = cliente.getNome(); vetor[2] = cliente.getTelefone(); vetor[3] = cliente.getCidade(); vetor[4] = cliente.getAtivoTxt(); model.addRow(vetor); } private void openModal() { ClienteView clienteView = new ClienteView(null, true); clienteView.setCliente(getSelecionado()); clienteView.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 Cliente 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.getNome(), 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("Cadastro de Cliente"); 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", "Nome", "Telefone", "Cidade", "Ativo" }) { 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); if (jTable.getColumnModel().getColumnCount() > 0) { jTable.getColumnModel().getColumn(3).setResizable(false); } 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, 914, Short.MAX_VALUE) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, 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(javax.swing.GroupLayout.DEFAULT_SIZE, 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 }