• Post Reply Bookmark Topic Watch Topic
  • New Topic

delegating event handling

 
Meghana Reddy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to develop a small login application.

I have a simple login gui called LoginFrame.java which contains a textfield and a password field and a submit button. When the user enters values in textfield and password and presses the Submit button, the textfield and password should be validated.

I want to design this in a way such that all the events generated by LoginFrame.java should be handled in another class called LoginFrameEventHandler.java which implements all the corresponding event listeners.
Ex: JButton submit = new JButton();
submit.addActionListener(new LoginFrameEventHandler());

Now when the submit button is pressed, the event is fired properly, but I am not able to get the reference to the textfield and the password field of LoginFrame.java in the LoginFrameEventHandler.java.

Any suggestions?


Thanks in advance

Meghana
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meghana Reddy:
I want to design this in a way such that all the events generated by LoginFrame.java should be handled in another class called LoginFrameEventHandler.java which implements all the corresponding event listeners.
Ex: JButton submit = new JButton();
submit.addActionListener(new LoginFrameEventHandler());

Now when the submit button is pressed, the event is fired properly, but I am not able to get the reference to the textfield and the password field of LoginFrame.java in the LoginFrameEventHandler.java.


Well you could have your listener accept those two fields in its constructor, so something like:
JButton submit = new JButton();
submit.addActionListener(new LoginFrameEventHandler(userField, passField));
 
Meghana Reddy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brian

I am in fact sending the reference of LoginFrame to the constructor of LoginFrameEventHandler since the requirement calls for disposing the LoginFrame once login is successful and then show another GUI.

Thanks for the help.

Meghana
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!