Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing App Doesn't End 1

Status
Not open for further replies.

procgod

Programmer
Dec 9, 2004
3
US
Hello, I am new to this site, but have been coding in Java for a while. I just started using the Swing Classes and have a problem. I have a large app, but can recreate the problem in a small app. The program is that the app appears to complete the main() but just hangs and does not end. I have recreated this problem in 1.4 and 1.5?? (5.0). Here's my short verison of code. Please send any ideas, I am sure I am missing something.

import javax.swing.*;

public class TestJava
{
public static void main(String[] args)
{
JDialog crap = new JDialog();
crap.setDefaultLookAndFeelDecorated(true);
crap.setModal(true);
crap.setVisible(true);

System.out.println("Exit");

}
}

I am running this from the command prompt, and I see "Exit", but then it just hangs.

Please help.
 
The programme will not exit until the modal dialog exits.

--------------------------------------------------
Free Database Connection Pooling Software
 
I do actually close the dialog box, but the program still does not end. Sorry, that was a relevant detail that I skipped. I really wish it was something that minor.
 
Hmmm, I added a default close operation , and a window listner, set to exit, but to no avail. I'm not really an expert at Swing. I guess it is due to the JDialog class threading off a thread to deal with producing the window - and this thread is not exiting on close.

--------------------------------------------------
Free Database Connection Pooling Software
 
Use a JFrame instead, seting the defualt close operation works on JFrames ... I don't know why it doesn't work JDialog.
 
When you create a JDialog without passing an owning Window in the constructor, Swing creates a hidden one for you. Trouble is this hidden Frame doesn't seem to want to go and holds the event Thread alive.

The solution for the code given above is to create your own hidden Frame and then dispose this after the modal dialog exits.

Code:
public static void main(String[] args)
   {
      [b][i]JFrame hiddenFrame = new JFrame();[/i][/b]
      JDialog crap = new JDialog([b][i]hiddenFrame[/i][/b]);
      crap.setDefaultLookAndFeelDecorated(true);
      crap.setModal(true);
      crap.setVisible(true);
      [b][i]hiddenFrame.dispose();[/i][/b]
      System.out.println("Exit");
      
   }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top