Results 1 to 4 of 4
- 12-10-2011, 09:32 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
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.
-
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
do,Java Code:if (fu == "bar") { // do something }
or,Java Code:if (fu.equals("bar")) { // do something }
Java Code:if (fu.equalsIgnoreCase("bar")) { // do something }
- 12-10-2011, 10:04 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
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.
-
Re: ActionListener to call a method from a different class?
MingShan's Hijack post deleted.
Similar Threads
-
Inner class method call
By d915172 in forum New To JavaReplies: 3Last Post: 11-11-2010, 09:40 PM -
Trying to call a method from sub class
By TheNewGuy in forum New To JavaReplies: 4Last Post: 10-17-2010, 07:08 AM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
How to call a method from another class?
By jboy in forum New To JavaReplies: 8Last Post: 09-09-2009, 07:29 AM -
How to call a class within a method
By Manfizy in forum New To JavaReplies: 3Last Post: 03-19-2009, 12:34 PM


LinkBack URL
About LinkBacks

Bookmarks