/*
   Chapter 9:     UserLogonTracking Class
   Programmer:   Brad Shedd
   Date:      April 22, 2004
   Program Name:  UserLogonTracking.java
   Purpose:      To provide a reusable UserLogon class
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class UserLogonTracking extends JFrame implements ActionListener
{
  
private JTextField jtfUser, jtfNumUsers;
  
private JButton jbtLogon, jbtLogoff;
  
private ArrayList userList;
  
private int numUsers = 0;

  
public static void main(String[] args)
   {
     
int width = 200;
     
int height = 130;
      UserLogonTracking frame =
new UserLogonTracking();
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(width, height);
      frame.centerOnScreen(width, height);
      frame.setVisible(
true);
   }

  
public UserLogonTracking()
   {
      userList =
new ArrayList(20); // sets up for 20 entries

      setTitle("User Tracking");

      JPanel p =
new JPanel();
      p.setLayout(
new FlowLayout());
      p.add(
new JLabel("User:   "));
      p.add(jtfUser =
new JTextField(10));

      p.add(jbtLogon =
new JButton("Log On"));
      p.add(jbtLogoff =
new JButton("Log Off"));

      p.add(
new JLabel("Number Of User:  "));
      p.add(jtfNumUsers =
new JTextField(5));
      jtfNumUsers.setEditable(
false);

      jbtLogon.addActionListener(
this);
      jbtLogoff.addActionListener(
this);

      getContentPane().setLayout(
new BorderLayout());
      getContentPane().add(p,BorderLayout.CENTER);

   }

  
public void centerOnScreen(int width, int height)
   {
     
int top, left, x, y;

     
// Get the screen dimension
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

     
// Determine the location for the top left corner of the frame
      x = (screenSize.width -  width)/2;
      y = (screenSize.height - height)/2;
      top = (x < 0) ? 0 : x;
      left = (y < 0) ? 0 : y;

     
// Set the frame to the specified location
      this.setLocation(top, left);
   }

  
public void actionPerformed(ActionEvent e)
   {
      String userEntered = jtfUser.getText();
     
if(userEntered.length() > 0)
      {
        
if(e.getSource() == jbtLogon)    // user clicked Use user
         {
        
try
         {
            logonUser(userEntered);
            jtfNumUsers.setText(String.valueOf(numUsers));
            jtfUser.setText(
"");
            jtfUser.requestFocus();
         }
        
catch (Exception ex)
             {
              JOptionPane.showMessageDialog(
this, ex.getMessage(), "Logon failed.",
 JOptionPane.ERROR_MESSAGE);

             }
         }
         
else if (e.getSource() == jbtLogoff)     // user clicked Change User
          {
         
try
          {
             logoffUser(userEntered);
             jtfNumUsers.setText(String.valueOf(numUsers));
             jtfUser.setText(
"");
             jtfUser.requestFocus();
         }
         
catch (Exception ex)
             {
               JOptionPane.showMessageDialog(
this, ex.getMessage(), "Logoff failed.",
 JOptionPane.ERROR_MESSAGE);
             }

          }
         }

        
else  // can never happen
         {
            JOptionPane.showMessageDialog(
this, "Please enter a valid user name.",
 
"Invalid entry. Try again.", JOptionPane.ERROR_MESSAGE);
            jtfUser.requestFocus();
          }

      }
// end of try

      private void logonUser(String user) throws Exception
     {
        
if(!userList.contains(user))
         {
            user.trim();
            userList.add(user);
            ++numUsers;
         }
        
else
            throw new Exception("User already logged on.");
      }

     
private void logoffUser(String user) throws Exception
      {
        
int index = 0;
         index = userList.indexOf(user.trim());
        
if(index < 0)
           
throw new Exception("User not logged on.");
        
else
         {
            userList.remove(index);
            --numUsers;
         }
      }
   }

Homepage