How to force a program to wait for a JFrame to be closed?

Hi, I want that code following the creation of a frame would not be executed until the frame is closed.For example:

publicclass test{

privatestaticvoid main (String args[]){

JFrame jf =new JFrame("test");

jf.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);

jTextField someData =new JTextField();

jf.getContentPane().add(someData);

jf.setVisible(true);

//I want that this code is executed only when

//the frame will be closed

System.out.println(someData.getText());

}

}

How can I achieve it ? Thanks.

[1044 byte] By [pattchena] at [2007-10-3 4:12:14]
# 1
Set your default action to be DISPOSE_ON_CLOSE and add a WindowListener, implementing the windowClosed() method to do what you want and then call System.exit(0).
itchyscratchya at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
Use a modal JDialog, not a JFrame.
camickra at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 3
Though if you're using a modal dialog without a parent frame then it'll be easy to lose on the desktop, since it won't have a taskbar item.
itchyscratchya at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Though if you're using a modal dialog without a parent frame then it'll

> be easy to lose on the desktop, since it won't have a taskbar item.

JFrame frame = new JFrame();

frame.setLocation(-100, 0);

.....

The use the above frame as the parent for your dialog.

camickra at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 5
Fudgetastic!
itchyscratchya at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 6

> > Though if you're using a modal dialog without a

> parent frame then it'll

> > be easy to lose on the desktop, since it won't have

> a taskbar item.

> > JFrame frame = new JFrame();

> frame.setLocation(-100, 0);

> .....

>

just wondering what happens if this frame contains a decent width (meaning > 100) component ;-)

Franck_Lefevrea at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 7
Well it doesn't, does it...
itchyscratchya at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 8

> just wondering what happens if this frame contains a decent width

Its a dummy frame. It doesn't contain any components. Its width consists of the 3 buttons on the title bar.So set the size to some value that will make it disappear.

You are correct though, I generally use -200 to be safe..

camickra at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 9
hijust wondering would the following line be a better option then going as far as -200 ...be at -100 and setframe. setUndecorated(true);
Trushanta at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 10
> would the following line be a better option Define "better".Why is positioning the frame at -100 better than -200? I have no idea.
camickra at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 11

hi

No offence meant but it is the same case as

> I generally use -200 to be safe....

If the 3 buttons go beyond size of 200 then would they appear on the edge of the screen..but setUndecorated(true) removes the button altogether removing the possiblity of buttons appearing on the screen, so i mentioned "better option" there... am i right camickr ?

Trushanta at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 12

I didn't question using the setUndecorated(...) if you wanted to.

I questioned why -100 was better then -200. They where just numbers I picked from my head (one when I answered this question today and the other was the actual value I used when I tested this out years ago).

The point of the suggestion was to create a frame and hide it.

camickra at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 13

Can't we just render this all moot by saying that if you set the y value negative and/or the x value to greater then the screen width then you're safe no matter how much space the frame occupies? :o)

(Technically, no, we can't, we'd have to check for multiple displays, but you get the idea - stick the frame to the right or below the visible area and it'll never encroach on the screen).

Anyway, I still think it's probably a bit too much of a fudge for my liking :o)

itchyscratchya at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 14

To get back to the OP, it's really the wrong question. You don't want a GUI program to wait. GUI programs are event driven. You have certain things to do when the user closes the window. So you "listen" for the window closer to be clicked and do those things, normally before actually closing the window.

The main thread, executing your program's main method, will just create and open the window and then finish. From then on what's running is the dispatcher thread which handles all the GUI gadgets etc..

Normally your program exits simply by closing it's last window, at which point the dispatcher thread ends.

What you code is something like:

final JTextField someData = new JTextField();

jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

jf.addWindowListener(new WindowAdapter() {

public void windowClosing(WIndowEvent evt) {

// this is where you do you window closing stuff

System.out.println(someData.getText();

}

});

jf.setVisible(true);

// main can now terminate

}

That's creating an instance of an "anoymous class" and attaching it to the window. The code in windowClosing is not run immediately, but when the window is closing. The someData variable has to be final for the anonymous class method to reference it.

malcolmmca at 2007-7-14 22:12:56 > top of Java-index,Desktop,Core GUI APIs...
# 15

Thanks for yours replies.

I talk about "waiting" because the main program needs the data provided by the user through gui to initialize connections (or other init) correctly. In fact, the code that must be executed after the window will be closed is not in the same method neither in the same class as the creation of the window.here what i'm talking about:

public class Main{

//These fields have getter and setter methods

private String url;

private String username;

private String pass;

public static void main (String args[]){

File f = new File("config.ini");

if (f.exists()){

// Initialize url,username... with data in the config file

InitWithConfiFile.init("config.ini");

}

else{

// Initialize url,username... with data iprovided by the user through the GUI

AskDataToUser.init();

}

//If the file doesn't exist and the program continues to run,this will provide null as parameters to ConnectToDatabase method

ConnectToDatabase(url,username,pass);

.......

......

}

The init method of the class AskDataToUser creates a window which asks some data to user (as the main i've submitted in my precedent post)and initialise url,username...fields.So, if the config file doesn't exist and the program doesn't "wait" for the window opened in the AskToUser.init() method to be closed,this program will never walk...

So,in this case how should I resolve my problem? Thanks.

pattchena at 2007-7-21 10:25:56 > top of Java-index,Desktop,Core GUI APIs...
# 16
> So,in this case how should I resolve my problem? See reply 2.If you need more information then read this section from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html]How to Use Dialogs[/url].
camickra at 2007-7-21 10:25:56 > top of Java-index,Desktop,Core GUI APIs...
# 17

Hi

My problem is solved.Thanks again.

I have used JDialog (thanks camickr ).

In the init method of class AskDataToUser,i have constructed a JDialog which contains some textfields and other swing stuff.Then i showed this JDialog using the following code:

JFrame owner = new JFrame();

owner.setLocation(-200,0); //-300 would be "better"?:)

//GUIDataInit extends JDialog and contains swing components for asking data to user

guiData = new GUIDataInit(owner,"Connexion parameters",true);

//The current class reacts if the user validate the Dialog

guiData.getJBtnValider().addActionListener(this);

guiData.setVisible(true);

And i'm happy!!!

Thanks again.

pattchena at 2007-7-21 10:25:56 > top of Java-index,Desktop,Core GUI APIs...