• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Event Handling

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im new to java & self-taught, so i may be missing something very basic. i wish to create a stand-alone logon dialog (frame) that can be invoked prior to any server program. The user would enter server, userid, & password, then press logon button. the logic would try to connect and return pass/fail, & 3 logon values above via a vector for future use.

I implement ActionListener on the Logon button and i find that action performed(actionevent e) cannot return a vector (or anything else). I want my logic in the main app, not in my logon dialog. there must be a way.

the question is how do i get out of listener mode (and the login program) without system.exit-ing?

thanks in advance to any who help. please be kind. i've only been writing code since 1965.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi AJ --

Welcome to JavaRanch!

If you're looking for a way to write something like

Vector tokens = login();

where login throws up a dialog box, waits for a response, and then returns the tokens, then one way to do it looks something like the following. I warn you that it's not the most trivial thing in the world to do.



In Java, event handlers run in a special thread created by the window toolkit. This is a different thread from the one main() runs in. This code assumes it will not be called from another event handler. What this code does is set up event handlers that will notify the main thread when a button has been pushed, and then puts the main thread (the thread that called login()) to sleep by calling wait() until that button-pushing happens. When a button is pushed, wait() returns, then login returns the Vector, which may or may not have some Credential objects in it.

I see that I've actually left out a (necessary) try/catch for InterruptedException around the wait() call.

Final note: don't use Vector, use ArrayList instead.
 
AJ Metc
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Earnest. You rock! I dabbled with wait/notify but didn't know how to use it properly. Wait/Notify did the trick. This is a major leap for me. It opens the door to the design strategies that I am accustomed to. Thank you for your help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic