tags:

views:

300

answers:

3

In a JFrame, when I click on 'login', I pop up another Jframe which is the login window.

How do I make my main Jframe wait for my login Jframe to exit, before doing anything else?

+4  A: 

Just use a modal dialog in stead of a frame, that way you cannot do anything else until it'is closed

see http://mindprod.com/jgloss/modal.html for explanation and see http://www.java2s.com/Tutorial/Java/0240%5F%5FSwing/ASimpleModalDialog.htm for code example

If you insist on using a JFrame, you could use a workaround by cover the other frame by a glassframe.. Not too a nice solution, I admit..

Peter
I would advise the same, but nonetheless, could you suggest how could be ANY JFrame made modal? I come from Delphi world, and there one would call myForm.showModal; instead of just myForm.show; Is there any simple way to achieve this effect?
Peter Perháč
Not that I know of, but my swing is very rusty, it has been years so don't trust me on this one
Peter
Modality is only available for classes that subclass java.awt.Dialog (like javax.swing.JDialog), classes that subclass just java.awt.Window can't be model.
Andreas_D
@Andreas_D, change your comment into an answer and I would give you an upvote
Peter Perháč
+2  A: 

I agree that a modal dialog would be the best option here, but if I were to answer this question in it's more general form, say:

How do I make one JFrame wait for another JFrame?

I would say the easiest way to acheive this is by registering and firing event listeners.

  • In your "child" frame, register the "main" frame as an event listener.
  • In your "main" frame,
    • implement your choice of listener, e.g. ActionListener
    • in the method called by the listener, e.g. actionPerformed, code the logic that handles what happens upon each of the actions it can respond to in the "child" frame.

One can easily implement this to a ny number of situations, including the login scenario described in the question.

bguiz
A: 

Use JModalFrame instead of JFrame

Dany