Search code examples
javaclassactionlistenerftp-client

How to save login information for an ActionListener Java


So I'm programming a simple FTP Client and I have a login GUI where you can enter the userid and password. The problem is I can't figure out how to save the userid and password entered in that class and then use that information in my other java class that connects to the actual ftp client.

Here is an excerpt of what I'm working with:

public void placeComponents(JPanel panel) 
      {
          panel.setLayout(null);

        // Creating JLabel
        JLabel userLabel = new JLabel("User");
        userLabel.setBounds(10,20,80,25);
        panel.add(userLabel);

        // Username Field
        JTextField userField = new JTextField(20);
        userField.setBounds(100,20,165,25);
        panel.add(userField);

     // Creating JLabel
        JLabel passLabel = new JLabel("Pass");
        passLabel.setBounds(10,40,80,25);
        panel.add(passLabel);

        // Pass Field
        JTextField passField = new JTextField(20);
        passField.setBounds(100,40,165,25);
        panel.add(passField);

        // Creating login button
        JButton loginButton = new JButton("login");
        loginButton.setBounds(10, 80, 80, 25); 
        panel.add(loginButton); 

        //Listen For Button Press
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                String userid = userField.getText();
                String password = passField.getText();

                    if(userid.isEmpty()) 
                    {
                    System.out.println("Your userid and/or password cannot be nothing.");
                    } 
                    else 
                    {
                    System.out.println(userid);                 

                    if(password.isEmpty()) 
                    {
                    System.out.println("Your password cannot be nothing.");
                    } 
                    else 
                    {
                    System.out.println(password);
                    }
                }
            }
        });
    }      

Solution

  • The problem is I can't figure out how to save the userid and password entered in that class and then use that information in my other java class that connects to the actual ftp client.

    Just instantiate the FTP Client Class instance in loginButton#actionListener#actionPereformed method and pass the user/pass to it, something like this:

    loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                String userid = userField.getText();
                String password = passField.getText();
    
                    if(userid.isEmpty()) 
                    {
                    System.out.println("Your userid and/or password cannot be nothing.");
                    } 
                    else 
                    {
                    System.out.println(userid);                 
    
                    if(password.isEmpty()) 
                    {
                    System.out.println("Your password cannot be nothing.");
                    } 
                    else 
                    {
                    System.out.println(password);
                    }
    
                    // -----------
                    YourFTPClient c = new YourFTPClient(user, pass);
                    c.anyMethod();
                }
            }
        });