0% found this document useful (0 votes)
16 views5 pages

Users

Uploaded by

manuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Users

Uploaded by

manuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

package com.mycompany.

views;

import com.mycompany.interfaces.DAOUsers;
import com.mycompany.proy.DAOUsersImpl;
import com.mycompany.proy.Dashboard;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;

public class Users extends javax.swing.JPanel {

/**
* Creates new form Users
*/
public Users() {
initComponents();
LoadUsers();
}
private void LoadUsers() {
try {
DAOUsers dao = new DAOUsersImpl();
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
dao.listar("").forEach((u) -> model.addRow(new Object[]{u.getId(),
u.getName(), u.getLast_name_p(), u.getLast_name_m(), u.getDomicilio(),
u.getTel()}));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 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() {

bg = new javax.swing.JPanel();
title = new javax.swing.JLabel();
userSearch = new javax.swing.JTextField();
btnBuscar = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
deleteButton = new javax.swing.JButton();
editButton = new javax.swing.JButton();
addButton = new javax.swing.JButton();

bg.setBackground(new java.awt.Color(255, 255, 255));

title.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N


title.setText("USUARIOS");

btnBuscar.setBackground(new java.awt.Color(18, 90, 173));


btnBuscar.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
btnBuscar.setForeground(new java.awt.Color(255, 255, 255));
btnBuscar.setText("Buscar");
btnBuscar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBuscarActionPerformed(evt);
}
});

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null}
},
new String [] {
"ID", "Nombre", "Apellido P.", "Apellido M.", "Domicilio",
"Telefono"
}
) {
boolean[] canEdit = new boolean [] {
false, true, true, true, true, true
};

public boolean isCellEditable(int rowIndex, int columnIndex) {


return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(jTable1);

deleteButton.setBackground(new java.awt.Color(18, 90, 173));


deleteButton.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
deleteButton.setForeground(new java.awt.Color(255, 255, 255));
deleteButton.setText("Borrar");
deleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});

editButton.setBackground(new java.awt.Color(18, 90, 173));


editButton.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
editButton.setForeground(new java.awt.Color(255, 255, 255));
editButton.setText("Editar");
editButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
editButtonActionPerformed(evt);
}
});

addButton.setBackground(new java.awt.Color(18, 90, 173));


addButton.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
addButton.setForeground(new java.awt.Color(255, 255, 255));
addButton.setText("Nuevo");
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout bgLayout = new javax.swing.GroupLayout(bg);


bg.setLayout(bgLayout);
bgLayout.setHorizontalGroup(
bgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bgLayout.createSequentialGroup()
.addContainerGap()
.addGroup(bgLayout.createParallelGroup(javax.swing.GroupLayout.Alig
nment.TRAILING)
.addComponent(jScrollPane1)
.addGroup(bgLayout.createSequentialGroup()
.addGap(427, 427, 427)
.addComponent(addButton,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.UNRELATED)
.addComponent(editButton,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.UNRELATED)
.addComponent(deleteButton,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
bgLayout.createSequentialGroup()
.addComponent(userSearch)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.RELATED)
.addComponent(btnBuscar))
.addComponent(title, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.PREFERRED_SIZE, 80,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(43, 43, 43))
);
bgLayout.setVerticalGroup(
bgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bgLayout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(title)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED
)
.addGroup(bgLayout.createParallelGroup(javax.swing.GroupLayout.Alig
nment.LEADING, false)
.addComponent(btnBuscar, javax.swing.GroupLayout.DEFAULT_SIZE,
40, Short.MAX_VALUE)
.addComponent(userSearch))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELAT
ED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
260, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(bgLayout.createParallelGroup(javax.swing.GroupLayout.Alig
nment.BASELINE)
.addComponent(editButton)
.addComponent(deleteButton)
.addComponent(addButton))
.addGap(35, 35, 35))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);


this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bg, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bg, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
}// </editor-fold>

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {

Dashboard.ShowJPanel(new UpUser());
}

private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {

if (jTable1.getSelectedRow() > -1) {


try {
int idUsuario = (int) jTable1.getValueAt(jTable1.getSelectedRow(),
0);
DAOUsers dao = new DAOUsersImpl();
Dashboard.ShowJPanel(new UpUser(dao.getUserById(idUsuario)));
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
javax.swing.JOptionPane.showMessageDialog(this, "Debes seleccionar el
usuario a editar.\n", "AVISO", javax.swing.JOptionPane.ERROR_MESSAGE);
}
}

private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {

DAOUsers dao = new DAOUsersImpl();


DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
if (jTable1.getSelectedRows().length < 1) {
javax.swing.JOptionPane.showMessageDialog(this, "Debes seleccionar uno
o más usuarios a eliminar.\n", "AVISO", javax.swing.JOptionPane.ERROR_MESSAGE);
} else {
for (int i : jTable1.getSelectedRows()) {
try {
dao.eliminar((int) jTable1.getValueAt(i, 0));
model.removeRow(i);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}

private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {

try {
DAOUsers dao = new DAOUsersImpl();
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);
dao.listar(userSearch.getText()).forEach((u) -> model.addRow(new
Object[]{u.getId(), u.getName(), u.getLast_name_p(), u.getLast_name_m(),
u.getDomicilio(), u.getTel()}));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

// Variables declaration - do not modify


private javax.swing.JButton addButton;
private javax.swing.JPanel bg;
private javax.swing.JButton btnBuscar;
private javax.swing.JButton deleteButton;
private javax.swing.JButton editButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JLabel title;
private javax.swing.JTextField userSearch;
// End of variables declaration
}

You might also like