permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes Instantiating a JDialog in constructor - won't execute other lines.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Instantiating a JDialog in constructor - won Watch "Instantiating a JDialog in constructor - won New topic
Author

Instantiating a JDialog in constructor - won't execute other lines..

Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
I took on the advice to use a JFrame for my first screen, and a JDialog for al the other screens. I use a strategy pattern to separate the looks from the behaviour. In the constructor of my behaviour class I instatiate the class for the looks:
Where TafelView is my new screen (JDialog)

Unfortunately, these lines won't run, because it stops on the line that makes the JDialog:

How can I do it so that these lines will run?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

I believe Dialogs are intended to prompt user input:
A Dialog window is an independent subwindow meant to carry temporary notice apart from the main Swing Application Window.

The Swing Tutorial: How to make Dialogs.
so they pause the execution of the thread they are spawned from.
If you don't want to wait for user input, I believe you'll want to use another Frame (or InternalFrame, if you want to block input to the main frame).


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
Well, I have a main frame, with a couple of buttons on it. When one of those buttons is clicked, a second window comes up. That second window has a form on it; a couple of labels + textfields, and two buttons "save" and "cancel". I first did it with a JFrame, but was advised to use a JDialog instead. But then this problem arises, and I don't know what to do know.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

Besjamain Greenaway wrote: I first did it with a JFrame, but was advised to use a JDialog instead.


I looked through your previous posts for that advice and couldn't find it. Why was a JDialog advised over a JFrame?
Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
Well, here is part of one it:
Wouldn't you be better off having a JDialog sitting above the main JFrame rather than a second JFrame? Then there is absolutely no danger of closing the app when the dialog closes, you can better control modality type, and you can guarantee that the subwindow always sits above the main one.


I read about it somewhere else as well, but I can't find it that quick, although I have a hunch that I read it on Sun's website somewhere. But I'm guessing that you say that it's okay?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

Google says it is here. I just wanted to see the context of your question and the answer.
I would not say using JDialog is "OK". It doesn't do what you want it to do. I think you have two options (there may be more):
1. use a JDialog with the understanding that processing in that thread halts
2. use a second JFrame or a JInternalFrame and continue processing in the background.
Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
Well, I guess I'm back to using JFrame again Thank you for answering my question.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

You can also make the dialog non-modal, that will also not block the method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
That's great, less adjusting! I'll go and find out what would be more preferrable then, because I don't really see why a JFrame or JDialog or visa versa apart from the modality.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Instantiating a JDialog in constructor - won't execute other lines..
 
Similar Threads
how to use getParent() ?
Why doesn't my WindowListener work?
JDialog and JFrame *HELP* !!!
JDialog and JFrame *HELP* !!!
Bringing my action listener in line