Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing close ONLY one application

Tags:

java

swing

Hi all: I have a Java Swing App. There is a button allow user to create open up a new window of the application. I use System.Exit(0) when user decides to close the application, however when I press "Close" button, both Application windows closed.

public static void main(String[] args)  
{       
 ghMain = new GreenHouseMain();     
}

Above is how I initialize the first application, then use the same code to create new GreenHouseMain Object to open second application window.

So my question is how do I close only one application window which the close button I pressed from?

Thanks all

like image 445
Jack Avatar asked Mar 14 '26 10:03

Jack


2 Answers

call dispose() instead of System.exit() on the Window object that you want to close. When there are no more visible windows, the Event Dispatch thread will exit.

like image 176
robert_x44 Avatar answered Mar 16 '26 22:03

robert_x44


I assume that both windows are JFrames. If so, it is better to have the second window be a JDialog, modal or non-modal depending on your requirements. If you need both windows open and want to be able to let the user select which to close, then perhaps both need to be dialogs, though I'm not 100% sure based on the information you've presented. If these suggestions don't solve your problem, then please provide us with more details on your exact requirements.

like image 33
Hovercraft Full Of Eels Avatar answered Mar 16 '26 23:03

Hovercraft Full Of Eels