Exemplo n.º 1
0
/** @author Jessica */
public class TelaProfissao extends javax.swing.JFrame {

  ProfissaoDao pd = new ProfissaoDao();
  BDMySql bd = BDMySql.getInstance();
  private JDialog formProfissao;

  public TelaProfissao() {
    initComponents();
  }

  /**
   * 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.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jtListaProfissoes = new javax.swing.JTable();
    btInserir = new javax.swing.JButton();
    btAtualizar = new javax.swing.JButton();
    btExcluir = new javax.swing.JButton();
    btSair = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jtListaProfissoes.setModel(
        new javax.swing.table.DefaultTableModel(
            new Object[][] {
              {null, null},
              {null, null},
              {null, null},
              {null, null}
            },
            new String[] {"ID", "Nome"}));
    jScrollPane1.setViewportView(jtListaProfissoes);

    btInserir.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    btInserir.setText("Inserir");
    btInserir.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            btInserirActionPerformed(evt);
          }
        });

    btAtualizar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    btAtualizar.setText("Atualizar");
    btAtualizar.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            btAtualizarActionPerformed(evt);
          }
        });

    btExcluir.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    btExcluir.setText("Excluir");
    btExcluir.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            btExcluirActionPerformed(evt);
          }
        });

    btSair.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    btSair.setText("Sair");
    btSair.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            btSairActionPerformed(evt);
          }
        });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                layout
                    .createSequentialGroup()
                    .addGroup(
                        layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(
                                layout
                                    .createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(
                                        jScrollPane1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        375,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(
                                layout
                                    .createSequentialGroup()
                                    .addGap(21, 21, 21)
                                    .addComponent(btInserir)
                                    .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btAtualizar)
                                    .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btExcluir)
                                    .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(btSair)))
                    .addContainerGap()));
    layout.setVerticalGroup(
        layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                layout
                    .createSequentialGroup()
                    .addContainerGap()
                    .addComponent(
                        jScrollPane1,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        133,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                    .addGroup(
                        layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btInserir)
                            .addComponent(btAtualizar)
                            .addComponent(btExcluir)
                            .addComponent(btSair))
                    .addGap(19, 19, 19)));

    pack();
  } // </editor-fold>//GEN-END:initComponents

  private void btInserirActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btInserirActionPerformed
    if (formProfissao == null) {
      // JFrame mainFrame = TelaPrincipal.getApplication().getMainFrame();
      formProfissao = new FormProfissao(this, true);
      formProfissao.setLocationRelativeTo(this);
    }
    formProfissao.setVisible(true);
    atualizaTabela();
  } // GEN-LAST:event_btInserirActionPerformed

  private void btAtualizarActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btAtualizarActionPerformed
    int i = jtListaProfissoes.getSelectedRow();

    if (i == -1) {
      JOptionPane.showMessageDialog(
          null, "Selecione uma linha da tabela", "Erro", JOptionPane.ERROR_MESSAGE);
    } else {
      int id = Integer.parseInt((String) jtListaProfissoes.getValueAt(i, 0));

      formProfissao = new FormProfissao(this, true, id);
      formProfissao.setLocationRelativeTo(this);
      formProfissao.setVisible(true);
      atualizaTabela();
    }
  } // GEN-LAST:event_btAtualizarActionPerformed

  private void btExcluirActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btExcluirActionPerformed
    Object[] options = {"Confirmar", "Cancelar"};
    int m =
        JOptionPane.showOptionDialog(
            null,
            "Deseja realmente exluir o registro selecionado?",
            "Informação",
            JOptionPane.DEFAULT_OPTION,
            JOptionPane.WARNING_MESSAGE,
            null,
            options,
            options[0]);
    if (m == 0) {
      int i = jtListaProfissoes.getSelectedRow();
      if (i == -1) {
        JOptionPane.showMessageDialog(
            null, "Selecione uma linha da tabela", "Erro", JOptionPane.ERROR_MESSAGE);
      } else {
        int id = Integer.parseInt((String) jtListaProfissoes.getValueAt(i, 0));
        pd.deletaProfissao(id);
        JOptionPane.showMessageDialog(
            null, "Registro excluído com sucesso", "Informação", JOptionPane.INFORMATION_MESSAGE);
        atualizaTabela();
      }
    }
  } // GEN-LAST:event_btExcluirActionPerformed

  private void btSairActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btSairActionPerformed
    System.exit(0);
  } // GEN-LAST:event_btSairActionPerformed

  /** @param args the command line arguments */
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(
        new Runnable() {

          public void run() {
            TelaProfissao t = new TelaProfissao();
            t.setTitle("Sistema de Evolução de pacientes");
            t.atualizaTabela();
            t.setVisible(true);
          }
        });
  }

  public void atualizaTabela() {
    Object[][] lista = pd.listaProfissoes();
    jtListaProfissoes.setModel(
        new javax.swing.table.DefaultTableModel(lista, new String[] {"id", "Nome"}) {});
    jtListaProfissoes.getColumnModel().getColumn(0).setPreferredWidth(50);
    jtListaProfissoes.getColumnModel().getColumn(0).setResizable(true);
    jtListaProfissoes.getColumnModel().getColumn(1).setPreferredWidth(150);
    jtListaProfissoes.getColumnModel().getColumn(1).setResizable(true);
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btAtualizar;
  private javax.swing.JButton btExcluir;
  private javax.swing.JButton btInserir;
  private javax.swing.JButton btSair;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTable jtListaProfissoes;
  // End of variables declaration//GEN-END:variables
}