I'm trying to write an external log-in class for my Adv Java class in school. It's an online section and the teacher is very hard to reach thus why I'm asking for help here :). But my problem is right now I have my log-in form and I have it accepting (not validating yet just accepting) a user name passing it to my external class adding it to my array (because later I need to be able to delete it from the array to "log-off". This is what I have so far for my external class:
import java.util.*;
public class Login
{
private ArrayList loginHistory;
public Login(String newLogin) throws Exception
{
loginHistory = new ArrayList();
set(newLogin);
}
public void set(String login) throws Exception
{
boolean loginAdded = true;
login = login.trim();
loginAdded = loginHistory.add(login);
}
}//end of Login class
and this is what I have for my application demo part
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginDemo
{
public static void main(String[] argv)
{
int width = 210;
int height = 140;
final demoFrame f = new demoFrame("LoginDemo");
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.centerOnScreen(width, height);
f.setVisible(true);
}
}//end class LoginDemo
class demoFrame extends JFrame implements ActionListener
{
Login login = null;
String userName;
Integer counter = 0;
JTextField userNameField;
JTextField userCountField;
JButton jbtLogOn, jbtLogOff;
public demoFrame(String title)
{
super(title);
JLabel label1 = new JLabel("User: ");
userNameField = new JTextField(10);
JLabel label2 = new JLabel("Number of Users: ");
userCountField = new JTextField(5);
userCountField.setEditable(false);
jbtLogOn = new JButton("Logon");
jbtLogOff = new JButton("Logoff");
jbtLogOn.addActionListener(this);
jbtLogOff.addActionListener(this);
JPanel userPanel = new JPanel(new BorderLayout (10,10));
userPanel.add(label1, BorderLayout.WEST);
userPanel.add(userNameField, BorderLayout.EAST);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(jbtLogOn);
buttonPanel.add(jbtLogOff);
JPanel userCountPanel = new JPanel(new BorderLayout(10,10));
userCountPanel.add(label2, BorderLayout.WEST);
userCountPanel.add(userCountField, BorderLayout.EAST);
JPanel contentPanel = new JPanel(new FlowLayout());
contentPanel.add(userPanel);
contentPanel.add(buttonPanel);
contentPanel.add(userCountPanel);
setContentPane(contentPanel);
InputMap map;
map = jbtLogOn.getInputMap();
if (map != null)
{
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false), "pressed");
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,true), "release");
}
map = jbtLogOff.getInputMap();
if (map != null)
{
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false), "pressed");
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,true), "release");
}
}
public void centerOnScreen(int width, int height)
{
int top, left, x, y;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
x = (screenSize.width - width)/2;
y = (screenSize.height - height)/2;
top = (x < 0) ? 0 : x;
left = (y < 0) ? 0 : y;
this.setLocation(top, left);
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource() == jbtLogOn)
{
userName = new String(userNameField.getText());
login = new Login(userName);
userNameField.setText("");
userNameField.requestFocus();
counter++;
userCountField.setText(counter.toString());
}
if(e.getSource() == jbtLogOff)
{
userNameField.setText("");
userNameField.requestFocus();
if(counter != 0)
{
counter--;
userCountField.setText(counter.toString());
}
else
{
JOptionPane.showMessageDialog(this, "No username yet logged in", "Invalid userName. Try Again.", JOptionPane.ERROR_MESSAGE);
}
}
}
catch (NullPointerException ex)
{
JOptionPane.showMessageDialog(this, "No username yet logged in", "Invalid userName. Try Again.", JOptionPane.ERROR_MESSAGE);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, ex.getMessage(), "Invalid username. Try again.", JOptionPane.ERROR_MESSAGE);
}
}
}//end class demoFrame
my problem is that every time I hit add it starts a new blank arrayList for my loginHistory which i see that it's doing it on line 10 in my external class, I just don't know how to change it functionally so that it doesn't do that and I can add multiple names through the list. Also if somebody could help me instead of having just a counter for my second label box ("Number of Users Logged in") (line 103 in LoginDemo), is there a way to make that text box read the amount that is in the arrayList so that it's always accurate in the count of how many is logged in. Thank you to anybody that can help me.