Raising Events

K

Kevin Munro

Hello, my application class is calling a login method of a login class. This
login method brings up a list of users from the database and makes a
JPasswordField and JButton available.

I've got an ActionListener on my JButton and this code goes to the database
and validates the user & password combination when the user clicks the
JButton.

All is well, but how do I tell my application class that the user has just
logged on?

In VB I would use the RaiseEvent method and pass in a user id but there
doesn't seem to be any way of passing events back to a calling class in
Java.

Any advice? I don't really want to make my dialogs modal.

Thanks, Kevin.
 
J

james d.

This sounds like an architectural design question more so than an API one.

One possible route you may want to consider:
Re-write your Login class as a singleton so that it bring up a dialog box
prompting the user to enter his/her username and password. Once the user
has entered this information, validate it against your database. You would
not want to do this in your constructor method because a constructor cannot
return a value. Instead, write a method in Login to do this. The return
value would be if the login was successful or not. Your application would
repeatedly invoke the Login class until it's return value is true.

i.e.:
class Login
{
public static boolean login(); // call this to raise the login dialog
window
}

then, in your application:

while(Login.login() == false);

// The user is now logged in. Keep in mind, Login.login() will block

If this doesn't help at all, sorry :)
 
Joined
Jan 31, 2011
Messages
1
Reaction score
0
While there are no intrinsic event handling in Java like in e.g. C#, there are nice ways to deal with events still. One way is to use the Observer pattern. By making your classes Observers and Observables, events may be managed using callback method invocations. Also you may simply create your own interfaces, and make your "observables" call methods on the objects implementing these interfaces. A third way is to use an eventing library such as javaEventing (code.google.com/p/javaEventing). This works much like in C#, where you define your events, register listeners for events, and trigger (raise) events.

All paths lead to Rome :)
-JA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top