kevinharper
December 10th, 2012, 03:04 AM
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LayoutManageUsers extends JPanel{
String[][] activeSpecialists = {};
private int userID;
private JPanel pnlFullName;
private JPanel pnlHandleUser;
private JPanel pnlActiveUser;
private JPanel pnlDContainer;
private JTextField txtNewUserFullName;
private JTextField txtNewUserName;
private JTextField txtNewUserPassword;
private MyDisableButton btnDisableUser;
public LayoutManageUsers(){
activeSpecialists = DBQueries.getInstance().getActiveSpecialists();
JPanel pnlTitle = new JPanel();
pnlTitle.setPreferredSize(new Dimension(500, 25));
pnlTitle.setBackground(Color.WHITE);
JLabel lblTitle = new JLabel("Active Console Users");
pnlTitle.add(lblTitle);
pnlDContainer = new JPanel();
pnlDContainer.setBackground(Color.WHITE);
//START: Dynamic list of active users
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(500, 500));
this.setBackground(Color.WHITE);
int evenOrOdd = 0;
int userID;
String userFullName;
for(int row = 0; row < activeSpecialists.length; row++) {
userID = Integer.parseInt(activeSpecialists[row][0]);
userFullName = activeSpecialists[row][1];
pnlActiveUser = new JPanel();
pnlActiveUser.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
pnlActiveUser.setPreferredSize(new Dimension(400, 37));
//user full name layout
pnlFullName = new JPanel();
pnlFullName.setLayout(new FlowLayout(FlowLayout.LEFT,0,5));
pnlFullName.setPreferredSize(new Dimension(190, 25));
JLabel lblActiveUser = new JLabel();
lblActiveUser.setText(userFullName);
pnlFullName.add(lblActiveUser);
//handle user button layout
pnlHandleUser = new JPanel();
pnlHandleUser.setLayout(new FlowLayout(FlowLayout.RIGHT,10,5));
pnlHandleUser.setPreferredSize(new Dimension(190, 35));
btnDisableUser = new MyDisableButton(userID);
btnDisableUser.setText("Disable");
pnlHandleUser.add(btnDisableUser);
//alternate row border
if(evenOrOdd%2 != 0)
setBackground("WHITE");
evenOrOdd++;
pnlActiveUser.add(pnlFullName);
pnlActiveUser.add(pnlHandleUser);
pnlDContainer.add(pnlActiveUser);
}
// END: Dynamic list of active users
this.add(pnlTitle, BorderLayout.NORTH);
this.add(pnlDContainer, BorderLayout.CENTER);
/***
* Admin panel @ bottom
**/
JPanel pnlAddUser = new JPanel();
pnlAddUser.setLayout(new FlowLayout(FlowLayout.RIGHT, 0,0));
pnlAddUser.setPreferredSize(new Dimension(500,75));
pnlAddUser.setBackground(Color.WHITE);
JPanel pnlNewUserTitle = new JPanel();
pnlNewUserTitle.setPreferredSize(new Dimension(500, 25));
pnlNewUserTitle.setBackground(Color.WHITE);
JLabel lblNewUserTitle = new JLabel();
lblNewUserTitle.setText("Create a new User");
pnlNewUserTitle.add(lblNewUserTitle);
//User Full Name
JPanel pnlNewUserFullName = new JPanel();
pnlNewUserFullName.setPreferredSize(new Dimension(140, 25));
pnlNewUserFullName.setBackground(Color.WHITE);
JLabel lblNewUserFullName = new JLabel("Name");
txtNewUserFullName = new JTextField();
txtNewUserFullName.setPreferredSize(new Dimension(55,20));
pnlNewUserFullName.add(lblNewUserFullName);
pnlNewUserFullName.add(txtNewUserFullName);
//Username
JPanel pnlNewUserName = new JPanel();
pnlNewUserName.setPreferredSize(new Dimension(140, 25));
pnlNewUserName.setBackground(Color.WHITE);
JLabel lblNewUserName = new JLabel("Username");
txtNewUserName = new JTextField();
txtNewUserName.setPreferredSize(new Dimension(55,20));
pnlNewUserName.add(lblNewUserName);
pnlNewUserName.add(txtNewUserName);
//Password
JPanel pnlNewUserPassword = new JPanel();
pnlNewUserPassword.setPreferredSize(new Dimension(140, 25));
pnlNewUserPassword.setBackground(Color.WHITE);
JLabel lblNewUserPassword = new JLabel("Password");
txtNewUserPassword = new JTextField();
txtNewUserPassword.setPreferredSize(new Dimension(55,20));
pnlNewUserPassword.add(lblNewUserPassword);
pnlNewUserPassword.add(txtNewUserPassword);
//Add user button
JButton btnAddUser = new JButton();
btnAddUser.setText("Add User");
btnAddUser.addActionListener(new addUser());
pnlAddUser.add(pnlNewUserTitle);
pnlAddUser.add(pnlNewUserFullName);
pnlAddUser.add(pnlNewUserName);
pnlAddUser.add(pnlNewUserPassword);
pnlAddUser.add(btnAddUser);
this.add(pnlAddUser, BorderLayout.SOUTH);
}
/***
* addUser() adds a new user when "Add User" button is clicked.
* Function verifies that no field was left blank.
**/
private class addUser implements ActionListener{
private boolean isFullNameValid = true;
private boolean isUserNameValid = true;
private boolean isPasswordValid = true;
String fullName;
String userName;
String password;
@Override
public void actionPerformed(ActionEvent ae){
isFullNameValid = chkFullName();
isUserNameValid = chkUserName();
isPasswordValid = chkPassword();
//display red backgrounds where errors found, return (exit)
if(isFullNameValid == false || isUserNameValid==false || isPasswordValid == false){
bgError(isFullNameValid, isUserNameValid, isPasswordValid);
return;
}
fullName = txtNewUserFullName.getText().trim();
userName = txtNewUserName.getText().trim();
password = txtNewUserPassword.getText().trim();
DBQueries.getInstance().addNewSpecialist(fullName, userName, password);
}
}
/***
* chkFullName() checks input from txtNewUserFullName.
* It returns true if input is valid and false if invalid is detected.
**/
private boolean chkFullName(){
boolean isValid = true;
if(txtNewUserFullName.getText().trim().equals(""))
isValid = false;
return isValid;
}
/***
* chkUserName() checks input from txtNewUserName.
* It returns true if input is valid and false if invalid is detected.
**/
private boolean chkUserName(){
boolean isValid = true;
if(txtNewUserName.getText().trim().equals(""))
isValid = false;
return isValid;
}
/***
* chkPassword() checks input from txtNewUserPassword.
* It returns true if input is valid and false if invalid is detected.
**/
private boolean chkPassword(){
boolean isValid = true;
if(txtNewUserPassword.getText().trim().equals(""))
isValid = false;
return isValid;
}
/***
* bgError() changes the background color of all text fields previously
* found to contain invalid data.
* @param validFullNameValid, validUserNameValid, validPasswordValid
**/
private void bgError(boolean validFullNameValid, boolean validUserNameValid, boolean validPasswordValid){
if(validFullNameValid == false){
txtNewUserFullName.setBackground(Color.RED);
}
if(validUserNameValid == false){
txtNewUserName.setBackground(Color.RED);
}
if(validPasswordValid == false){
txtNewUserPassword.setBackground(Color.RED);
}
}
/***
* setBackground() alternates background color or "rows" that display active users'
* names. It sets all panels' backgrounds to GRAY or WHITE.
*/
private void setBackground(String color){
if(color.trim().equals("WHITE")){
pnlActiveUser.setBackground(Color.WHITE);
pnlFullName.setBackground(Color.WHITE);
pnlHandleUser.setBackground(Color.WHITE);
}
}
}
This above class displays a list of active users. The rows alternate in color (white & gray). Each row has its own button for deactivation. When clicked, the corresponding user is disabled from the system.
Two questions...
How do I get the corresponding row to be removed from view when the user is removed? I want the white/gray color scheme to stay true. Instead of removing a row do I have to instead "refresh" the user panel?
I also want to add an "Edit" button but how do I let the userEdit panel know the user id of the user I want to edit? I was hoping to create one panel that would accept an id and then pull information for that one user from the DB but this has proven to be a little more difficult than I thought.
Any suggestions? Any links?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LayoutManageUsers extends JPanel{
String[][] activeSpecialists = {};
private int userID;
private JPanel pnlFullName;
private JPanel pnlHandleUser;
private JPanel pnlActiveUser;
private JPanel pnlDContainer;
private JTextField txtNewUserFullName;
private JTextField txtNewUserName;
private JTextField txtNewUserPassword;
private MyDisableButton btnDisableUser;
public LayoutManageUsers(){
activeSpecialists = DBQueries.getInstance().getActiveSpecialists();
JPanel pnlTitle = new JPanel();
pnlTitle.setPreferredSize(new Dimension(500, 25));
pnlTitle.setBackground(Color.WHITE);
JLabel lblTitle = new JLabel("Active Console Users");
pnlTitle.add(lblTitle);
pnlDContainer = new JPanel();
pnlDContainer.setBackground(Color.WHITE);
//START: Dynamic list of active users
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(500, 500));
this.setBackground(Color.WHITE);
int evenOrOdd = 0;
int userID;
String userFullName;
for(int row = 0; row < activeSpecialists.length; row++) {
userID = Integer.parseInt(activeSpecialists[row][0]);
userFullName = activeSpecialists[row][1];
pnlActiveUser = new JPanel();
pnlActiveUser.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
pnlActiveUser.setPreferredSize(new Dimension(400, 37));
//user full name layout
pnlFullName = new JPanel();
pnlFullName.setLayout(new FlowLayout(FlowLayout.LEFT,0,5));
pnlFullName.setPreferredSize(new Dimension(190, 25));
JLabel lblActiveUser = new JLabel();
lblActiveUser.setText(userFullName);
pnlFullName.add(lblActiveUser);
//handle user button layout
pnlHandleUser = new JPanel();
pnlHandleUser.setLayout(new FlowLayout(FlowLayout.RIGHT,10,5));
pnlHandleUser.setPreferredSize(new Dimension(190, 35));
btnDisableUser = new MyDisableButton(userID);
btnDisableUser.setText("Disable");
pnlHandleUser.add(btnDisableUser);
//alternate row border
if(evenOrOdd%2 != 0)
setBackground("WHITE");
evenOrOdd++;
pnlActiveUser.add(pnlFullName);
pnlActiveUser.add(pnlHandleUser);
pnlDContainer.add(pnlActiveUser);
}
// END: Dynamic list of active users
this.add(pnlTitle, BorderLayout.NORTH);
this.add(pnlDContainer, BorderLayout.CENTER);
/***
* Admin panel @ bottom
**/
JPanel pnlAddUser = new JPanel();
pnlAddUser.setLayout(new FlowLayout(FlowLayout.RIGHT, 0,0));
pnlAddUser.setPreferredSize(new Dimension(500,75));
pnlAddUser.setBackground(Color.WHITE);
JPanel pnlNewUserTitle = new JPanel();
pnlNewUserTitle.setPreferredSize(new Dimension(500, 25));
pnlNewUserTitle.setBackground(Color.WHITE);
JLabel lblNewUserTitle = new JLabel();
lblNewUserTitle.setText("Create a new User");
pnlNewUserTitle.add(lblNewUserTitle);
//User Full Name
JPanel pnlNewUserFullName = new JPanel();
pnlNewUserFullName.setPreferredSize(new Dimension(140, 25));
pnlNewUserFullName.setBackground(Color.WHITE);
JLabel lblNewUserFullName = new JLabel("Name");
txtNewUserFullName = new JTextField();
txtNewUserFullName.setPreferredSize(new Dimension(55,20));
pnlNewUserFullName.add(lblNewUserFullName);
pnlNewUserFullName.add(txtNewUserFullName);
//Username
JPanel pnlNewUserName = new JPanel();
pnlNewUserName.setPreferredSize(new Dimension(140, 25));
pnlNewUserName.setBackground(Color.WHITE);
JLabel lblNewUserName = new JLabel("Username");
txtNewUserName = new JTextField();
txtNewUserName.setPreferredSize(new Dimension(55,20));
pnlNewUserName.add(lblNewUserName);
pnlNewUserName.add(txtNewUserName);
//Password
JPanel pnlNewUserPassword = new JPanel();
pnlNewUserPassword.setPreferredSize(new Dimension(140, 25));
pnlNewUserPassword.setBackground(Color.WHITE);
JLabel lblNewUserPassword = new JLabel("Password");
txtNewUserPassword = new JTextField();
txtNewUserPassword.setPreferredSize(new Dimension(55,20));
pnlNewUserPassword.add(lblNewUserPassword);
pnlNewUserPassword.add(txtNewUserPassword);
//Add user button
JButton btnAddUser = new JButton();
btnAddUser.setText("Add User");
btnAddUser.addActionListener(new addUser());
pnlAddUser.add(pnlNewUserTitle);
pnlAddUser.add(pnlNewUserFullName);
pnlAddUser.add(pnlNewUserName);
pnlAddUser.add(pnlNewUserPassword);
pnlAddUser.add(btnAddUser);
this.add(pnlAddUser, BorderLayout.SOUTH);
}
/***
* addUser() adds a new user when "Add User" button is clicked.
* Function verifies that no field was left blank.
**/
private class addUser implements ActionListener{
private boolean isFullNameValid = true;
private boolean isUserNameValid = true;
private boolean isPasswordValid = true;
String fullName;
String userName;
String password;
@Override
public void actionPerformed(ActionEvent ae){
isFullNameValid = chkFullName();
isUserNameValid = chkUserName();
isPasswordValid = chkPassword();
//display red backgrounds where errors found, return (exit)
if(isFullNameValid == false || isUserNameValid==false || isPasswordValid == false){
bgError(isFullNameValid, isUserNameValid, isPasswordValid);
return;
}
fullName = txtNewUserFullName.getText().trim();
userName = txtNewUserName.getText().trim();
password = txtNewUserPassword.getText().trim();
DBQueries.getInstance().addNewSpecialist(fullName, userName, password);
}
}
/***
* chkFullName() checks input from txtNewUserFullName.
* It returns true if input is valid and false if invalid is detected.
**/
private boolean chkFullName(){
boolean isValid = true;
if(txtNewUserFullName.getText().trim().equals(""))
isValid = false;
return isValid;
}
/***
* chkUserName() checks input from txtNewUserName.
* It returns true if input is valid and false if invalid is detected.
**/
private boolean chkUserName(){
boolean isValid = true;
if(txtNewUserName.getText().trim().equals(""))
isValid = false;
return isValid;
}
/***
* chkPassword() checks input from txtNewUserPassword.
* It returns true if input is valid and false if invalid is detected.
**/
private boolean chkPassword(){
boolean isValid = true;
if(txtNewUserPassword.getText().trim().equals(""))
isValid = false;
return isValid;
}
/***
* bgError() changes the background color of all text fields previously
* found to contain invalid data.
* @param validFullNameValid, validUserNameValid, validPasswordValid
**/
private void bgError(boolean validFullNameValid, boolean validUserNameValid, boolean validPasswordValid){
if(validFullNameValid == false){
txtNewUserFullName.setBackground(Color.RED);
}
if(validUserNameValid == false){
txtNewUserName.setBackground(Color.RED);
}
if(validPasswordValid == false){
txtNewUserPassword.setBackground(Color.RED);
}
}
/***
* setBackground() alternates background color or "rows" that display active users'
* names. It sets all panels' backgrounds to GRAY or WHITE.
*/
private void setBackground(String color){
if(color.trim().equals("WHITE")){
pnlActiveUser.setBackground(Color.WHITE);
pnlFullName.setBackground(Color.WHITE);
pnlHandleUser.setBackground(Color.WHITE);
}
}
}
This above class displays a list of active users. The rows alternate in color (white & gray). Each row has its own button for deactivation. When clicked, the corresponding user is disabled from the system.
Two questions...
How do I get the corresponding row to be removed from view when the user is removed? I want the white/gray color scheme to stay true. Instead of removing a row do I have to instead "refresh" the user panel?
I also want to add an "Edit" button but how do I let the userEdit panel know the user id of the user I want to edit? I was hoping to create one panel that would accept an id and then pull information for that one user from the DB but this has proven to be a little more difficult than I thought.
Any suggestions? Any links?