Results 1 to 4 of 4
  1. #1
    dacoolest is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Question ActionListener to call a method from a different class?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class LoginGui {
    
    	public JFrame frame;
    	public JPanel jp;
    	public JTextField username;
    	public JPasswordField password;
    	public JButton login;
    	
    	
    	public LoginGui(){
    		
    		frame = new JFrame("Login");
    		jp = new JPanel();
    		frame.setContentPane(jp);
    		username = new JTextField("username");
    		password = new JPasswordField("password");
    		login = new JButton("Login");
    		login.addActionListener(new MyActionListener() );
    		
    		
    		frame.getContentPane().add(username);
    		frame.getContentPane().add(password);
    		frame.getContentPane().add(login);
    		
    		frame.setSize(400,400);
    		frame.setVisible(true);
    		
    	}
    	
    	
    	private class MyActionListener implements ActionListener {
    		
    		public void actionPerformed(ActionEvent e){
    			
    			String nm = username.getText();
    			String pw = password.getText();
    			
    			
    		}
    		
    	}	
    }
    
    public class user {
    
    	public String name;
    	public String password;
    	
    	public user(){
    		name = new String();
    		password = new String();
    	}
    	
    	public void validate(String n, String p){
    		if (name == n){
    			if (password == p){
    				System.out.println("valid");
    			}
    		}
    	}
    }
    public class Session {
    	
    	public LoginGui log;
    	
    	public user us;
    	
    	public Session(){
    		us = new user();
    		us.name = "myname";
    		us.password = "mypass";
    		
    		log = new LoginGui();
    	}
    }
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Session s = new Session();
    
    	}
    }

    The LoginGui class has an ActionListener class which should call a method (validate) from the user class. but I cannot call that method from the ActionListener class since its the user class object is declared in the Session class.

    I'm getting similar type problems everywhere in my program and it would be a great help if you can give me an idea about how to solve it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener to call a method from a different class?

    I'm not sure what the overall configuration of your program is supposed to be. So the main method creates a Session object which launches the GUI. The GUI interacts with a user who tries to log in. Is the login supposed to be verified by a method in Session? If so, give your LoginGui a Session variable, a Session parameter for its constructor with which it initializes its Session variable, and pass in the Session object (this from within the Session class code) into the LoginGui's constructor call. Then the LoginGui can call Session's verify method.

    Also, another problem with your code is that you're using == to check for String equivalence rather than use either the equals(...) or the equalsIgnoreCase(...) method. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

    Java Code:
    if (fu == "bar") {
      // do something
    }
    do,

    Java Code:
    if (fu.equals("bar")) {
      // do something
    }
    or,

    Java Code:
    if (fu.equalsIgnoreCase("bar")) {
      // do something
    }

  3. #3
    dacoolest is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: ActionListener to call a method from a different class?

    Thanks a lot for the reply. That solved the problem and once again I can proceed with writing the program.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Inner class method call
    By d915172 in forum New To Java
    Replies: 3
    Last Post: 11-11-2010, 10:40 PM
  2. Trying to call a method from sub class
    By TheNewGuy in forum New To Java
    Replies: 4
    Last Post: 10-17-2010, 08:08 AM
  3. Replies: 2
    Last Post: 05-01-2010, 09:59 AM
  4. How to call a method from another class?
    By jboy in forum New To Java
    Replies: 8
    Last Post: 09-09-2009, 08:29 AM
  5. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 01:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •