I am doing some basic Java Swing application (beginner level) . what i have to do is when I press a button that opens another JFrame(popup) it opens, but if clicked again while the pop up is still open it opens again and I have 2 popups how can I when I click a button to open a different JFrame make the program check if it is already open so it doesn't open again I don't want to close the main JFrame that has the button I just want the popup doesn't keep opening if it is already open
Asked
Active
Viewed 23 times
0
-
A second JFrame should never be used for a secondary pop-up window. In this situation, the standard practice is to use a JDialog instead, and it sounds like you want to probably make it a modal JDialog, one which will prevent the user from interacting with the main window (a JFrame) until it has been closed. – Hovercraft Full Of Eels Dec 31 '21 at 06:37
-
Otherwise, if you don't want the second window to be modal, then one solution is to again, create and display a JDialog, but a non-modal one, and create a single instance of it, so even if the user tries to open a new one, only the original single dialog will display. – Hovercraft Full Of Eels Dec 31 '21 at 07:00
-
@HovercraftFullOfEels sorry i wasn't clear in my question but what it's not a popup it's just a small jframe i open it when i click on a button on my mainframe which will be always open(which i want) and i write something in that jframe and close it but if i clicked the button again the jframe opens again while the old jframe is still open and if i clicked the button again the jframe opens again while the old 2 jframes are still open – Mahmoud Samy Dec 31 '21 at 08:33
-
Fine, then again, it should be a JDialog, but a non-modal JDialog. Your application should have only one main application window (the JFrame) and then can have as many dialog or sub-windows as needed. – Hovercraft Full Of Eels Dec 31 '21 at 12:10