I am trying to get my text inputted from my JTextfields into my object in the Email_GUI class. It will then get passed into the contructor in my User class. The problem I am having is that when I enter information into the textfield, Null or nothing but spaces is passed.
public class Email_GUI extends JFrame {
// Fields
// Labels
private JLabel FNameL = new JLabel ("First Name:");
private JLabel LNameL = new JLabel ("Last Name:");
private JLabel DepL = new JLabel ("Department:");
private JLabel PassL = new JLabel ("Password (at least 8-10 characters):");
private JLabel CenterB = new JLabel("Make Selection", SwingConstants.CENTER);
private JLabel Idlabel = new JLabel ("ID");
//Jbutton
private JButton CreateB = new JButton ("Create");
private JButton CreateUB = new JButton ("Create User");
private JButton ViewUB = new JButton ("Search Account");
private JButton CancelB = new JButton ("Cancel");
//JTextFIeld
private JTextField FNameTF = new JTextField (20);
private JTextField LNameTF = new JTextField (20);
private JTextField DepTF = new JTextField (20);
private JTextField PassTF = new JTextField (20);
private JTextField IDTF = new JTextField (20);
private String FirstName = FNameTF.getText().trim();
private String LastName = LNameTF.getText().trim();
private String Department = DepTF.getText().trim();
private String Password = PassTF.getText().trim();
private User UserInformation = new User (FirstName, LastName, Department, Password);
// GUI
public Email_GUI () {
super("Email Account");
this.setSize(700, 350);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
setVisible(true);
Container mainContainer = this.getContentPane();
mainContainer.setBackground(Color.LIGHT_GRAY);
mainContainer.setLayout(new BorderLayout(8,6));
this.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.LIGHT_GRAY));
MatteBorder Border = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black);
TitledBorder TBorder = BorderFactory.createTitledBorder(Border, "Navigation Menu");
Border EBorder = BorderFactory.createEmptyBorder(6, 6, 6, 6);
CompoundBorder CBorder = BorderFactory.createCompoundBorder(TBorder, EBorder);
//Panel Left
JPanel LeftPanel = new JPanel();
LeftPanel.setPreferredSize(new Dimension(150, 150));
LeftPanel.setBorder(CBorder);
LeftPanel.add(Box.createRigidArea(new Dimension(3,0)));
LeftPanel.setLayout(new BoxLayout(LeftPanel, BoxLayout.PAGE_AXIS));
LeftPanel.setBackground(Color.WHITE);
LeftPanel.add(CreateUB);
CreateUB.setMaximumSize(new Dimension(200, 35));
CreateUB.setFocusable(false);
LeftPanel.add(Box.createVerticalStrut(5));
LeftPanel.add(ViewUB);
ViewUB.setMaximumSize(new Dimension(350, 35));
ViewUB.setFocusable(false);
mainContainer.add(LeftPanel, BorderLayout.WEST);
//Panel Middle
CenterB.setOpaque(true);
CenterB.setBorder(new LineBorder(Color.BLACK,1));
mainContainer.add(CenterB);
CenterB.setBackground(Color.LIGHT_GRAY);
CenterB.setVisible(true);
// Listeners and events for processesing a request for the GUI
CreateUB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
CreateUB.setEnabled(false);
CenterB.setVisible(false);
Border LBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
Border EBorder = BorderFactory.createEmptyBorder(30, 30, 30, 30);
CompoundBorder CBorder = BorderFactory.createCompoundBorder(LBorder, EBorder);
JPanel MiddlePanel = new JPanel();
MiddlePanel.setBorder(CBorder);
MiddlePanel.setLayout(new GridLayout(8, 2, 3, 3));
MiddlePanel.setBackground(Color.WHITE);
MiddlePanel.setVisible(true);
MiddlePanel.add(FNameL);
MiddlePanel.add(FNameTF);
FNameTF.setMaximumSize(new Dimension(100, 20));
MiddlePanel.add(LNameL);
MiddlePanel.add(LNameTF);
LNameTF.setMaximumSize(new Dimension(100,20));
MiddlePanel.add(DepL);
MiddlePanel.add(DepTF);
DepTF.setMaximumSize(new Dimension(100, 20));
MiddlePanel.add(PassL);
MiddlePanel.add(PassTF);
PassTF.setMaximumSize(new Dimension(100, 20));
MiddlePanel.add(Box.createHorizontalBox());
MiddlePanel.add(Box.createHorizontalBox());
MiddlePanel.add(CreateB);
MiddlePanel.add(CancelB);
MiddlePanel.add(Box.createVerticalStrut(60));
mainContainer.add(MiddlePanel, BorderLayout.CENTER);
CancelB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
CreateUB.setEnabled(true);
MiddlePanel.setVisible(false);
CenterB.setVisible(true);
}
});
CreateB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
if(UserInformation.UserRecord.containsValue(UserInformation)){
JOptionPane.showMessageDialog(null,"User alread exists ","Warning",JOptionPane.PLAIN_MESSAGE );
}
else{
UserInformation.StoreUserInfo();
JOptionPane.showMessageDialog(null,"User added! ","",JOptionPane.PLAIN_MESSAGE );
}
}
catch(NumberFormatException ex){
}
}
});
}
});
public class User {
//Fields
private String FirstName;
private String LastName;
private String Department;
private String Password;
private int UserID;
private static final AtomicInteger IDCount = new AtomicInteger(0);
//Constructor that processe user information
public User (String FName, String LName, String Depart, String Pass){
this.FirstName = FName;
this.LastName = LName;
this.Department = Depart;
this.Password = Pass;
}
public HashMap<Integer, String> UserRecord = new HashMap<Integer, String>();
public void setFirstName(String FirstName){
this.FirstName = FirstName;
}
public void setLastName(String LastName){
this.LastName = LastName;
}
public void setDepartment(String Department){
this.Department = Department;
}
public void setPassword(String Password){
this.Password = Password;
}
// get methods
public String getFirstName(){
return FirstName;
}
public String getLastName(){
return LastName;
}
public String getDepartment(){
return Department;
}
public String getPassword(){
return Password;
}
public void StoreUserInfo (){
UserID = IDCount.incrementAndGet();
this.UserRecord.put(UserID,getFirstName()+getLastName()+getDepartment()+getPassword());
System.out.println(Arrays.asList(UserRecord));
}
}