Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with JDialog Close Operation  RSS feed

 
Farhad Rahmati
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys

I am wondering how the entire application could be close when you click on X in a JDialog Box.

I have tried

System.Exit (0)

but it only close the Dialog box

Cheers
Farhad
 
Mohamed Sanaulla
Bartender
Posts: 3182
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you adding the line: System.exit(0)? You should be adding it in the Window event listener for that JDialog.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried

System.Exit (0)

You couldn't have, since that won't compile (short of your having your own class named System with a badly named method).

Show us, in the form of an SSCCE (<--link) what you actually did try. But first, go through this:
http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html
 
Farhad Rahmati
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what do you guys recommend me to do

and how can I close the whole application with clicking on X in a diaglog box
 
Mohamed Sanaulla
Bartender
Posts: 3182
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl has already suggested what to be done- Add a WindowListener to your JDialog and look at the events supported by the same. See where you can add your call to exit the application.
 
Farhad Rahmati
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys,

I have already tried that, it works but it doesn't work for my application becouse

the dialog box asks for user name and password and it contains a Login Button, and checks weather the user name and password is correct or incorrect,
if it is correct it close the Dialog box and allows you to use the application, the dialog box is modal dialog box and block the whole application until you type the correct user name and password. if it's correct. it close the dialog box.

So, now if I add windowListener, even the username and password is correct. it close the dialog box and ends with closing the whole application which is nothing that I want.

I might be wrong with the use of Modal Dialog box as user name and password form which blocks the whole application.

if there is an alternative to make a username and password form which comes before the application starts as I couldn't find a way to do it. now the application starts and it call the modal dialog box. and when you click on X button the dialog box close and lets you use the application.

I tried (Redecorate = false) which removes the whole title bar in Dialog box. it is okay and kindly solved my problem. but it raise another problem. which is the Screen Keyboard.

my application has a screen key board for touch Screen computers. when you click on KeyBoard Button in Dialog box to open the keyboard to type user name and password. the modal dialog box blocks whole application including keyboard which is a separate dialog box.

Thanks for you suggestions. if you get the chance to read my long message and you could find a solution. Please let me know

Regards
Farhad
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, now if I add windowListener, even the username and password is correct. it close the dialog box and ends with closing the whole application

A WindowListener doesn't close the application. Your implementation of it does.

edit: From your problem description, you need to conditionally close the application when the username/password is incorrect, and not otherwise.

Frankly, it looks like you've been copying code snippets from here and there, throwing this mud at the wall and hoping it'll stick. Maybe you just need to go through some tutorials.
http://download.oracle.com/javase/tutorial/uiswing/index.html

Believe me, coding is much more fun when you know what you're doing.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankly speaking, I find this design horrible from the usability perspective. If the user inputs the wrong credentials, the ideal way is to provide a feedback to the user about it and let him provide the proper credentials all over again. If the application is going to terminate, just because I made a typo, then I am sure I wouldn't like the application.

Also, what Darryl said. +1
 
Farhad Rahmati
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daryl and Maneesh

Thanks for your suggestions, Indeed I have my problems as I am new in Java

I am not copying code from here and there. ofcourse I know what I am doing.

my problems I have less information about API and the Java Library

I know that windowListener does not close the application. the DialogBox window listener does ( JDialogBox1.dispose() ) and that is fine.

so, I thought, if the dialog box Dispose then the application should be closed, so I add windows listener when the dialog box dispose (System.exit(0))

the application lets the user to enter username/password three times. if he failed the application automatically get closed. that is fine and working
so if the users input is correct. again I need to dispose the dialog box. and this also close the application. I managed to solved the problem with disabling the title bar in Dialog box.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a feeling you're overcomplicating the issue. Here's an oversimplified version of my approach.
 
Farhad Rahmati
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke

Thanks alot for your code, I got better Idea now
 
Odd Steen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do this which I found easier, was to subclass JDialog and override processWindowEvent and setDefaultCloseOperation methods.

In processWindowEvent add this to the switch statement

In setDefaultCloseOperation add this to the if statement

and change the IllegalArgumentException to include EXIT_ON_CLOSE as below


Cheers,

Odd
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have broken the long line in your code tags. Are you sure it is safe to use System.exit() at all?
 
Odd Steen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fine.

Well, I'm not a Java expert and I understand that there is some caution about System.exit(). In my case I have a login dialog called from the application (a Frame). The login has only an Ok button and no cancel button. The login is not really to the application, but to the database the application connects to. So, if I start the application and clicks Ok without filling in any credentials, the application starts without any db connection. Hence, If a user starts the application by accident or changes his/her mind about it, I want the default closing operation to be exit so the application doesn't start unnecessarily. Hide or dispose will not solve that. I could have had a cancel button too and solved the problem more easily, but in this case I wanted the default close to be to quit the application.

In my case I see no problems with it and it works as expected. Is there any problem I should expect?

Regards,

Odd
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you use System.exit() you will close the running JVM immediately. Will that allow one to log off the database? Will the database log one off automatically? Is there a risk of connections being left open at the database end? If so you will gradually exhaust connectivity to the database and suffer a resource leak. You can probably recover the leak by turning it off and on again, but you probably don't want to. You might, by the way, have the same problems with EXIT_ON_CLOSE.
I think you will have to create a Listener or other code which closes the connections and disposes the GUI before terminating the JVM.
 
Odd Steen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my case, If I close the login dialog using the default closing operation and thus System.exit() no db connection is ever created. But if I do the same with the application, after having logged in and acquired a db connection, the situations you describe might well happen. I haven't thought of that to be honest and thank you for the comments. I'll have to fix this!

Regards,

Odd
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!