0

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));
 
 
}




        
 

}

CLick Here for Output of Code Screenshot

camickr
  • 321,443
  • 19
  • 166
  • 288
WilljLove
  • 1
  • 1
  • 5
    1) Variable names should NOT start with an upper case character. Follow examples used in your text book or tutorial. 2) You can't invoke the getText() method until the user has actually typed text into the text field. So the getText() method needs to be invoked in your ActionListener of your "Create User" button. Then you get the text from all the text fields and create the `User`. – camickr Jun 05 '21 at 20:07
  • 2
    You are more likely to get (good) answers if you post a [mcve] so that people don't have to dig through all the extra code. `User` could only have a name, for demonstration purposes, and with that the whole GUI should be much smaller and simpler. See also [ask]. – Robert Jun 05 '21 at 20:10
  • 3
    See the `TextDemo` example from the Swing tutorial on [How to Use Text Fields](https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html) for a working example that copies the text from the text field to the text area. – camickr Jun 05 '21 at 20:11
  • **General tips:** 1) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 2) **One** blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have keyboard shortcut specifically for formatting code. – Andrew Thompson Jun 06 '21 at 03:43

0 Answers0