-1

Close a previous window when I press a specific button in java how can i do this

I mean after I click on the registration I want the window called "Yuval Library " to close

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Yuval Dahan
  • 1
  • 1
  • 2
  • First stop should be the [JavaDocs](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html), the second stop should be [one or more tutorials](https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html), third step should be some searching/research. Your problem is not unique and has been asked/answered many times before – MadProgrammer Mar 20 '18 at 22:30
  • 1
    At some point you're going to find that having multiple windows is a pain in the code and limits re-usability - at that point, you should stop using `JFrame` as the base component and start using something like `JPanel` and simply add them to what ever container you want. Then something like [`CardLayout`](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) will become more useful – MadProgrammer Mar 20 '18 at 22:31
  • @MadProgrammer me wonders if those 'multiple frames' declare the default close operation as `EXIT_ON_CLOSE` and if that is adding a further complication to the OP's inquiry. But a) can't tell for sure with no MCVE. b) it doesn't change the fact the OP should be limiting it to a single `JFrame` and either dialogs or card layouts etc. for the other views. – Andrew Thompson Mar 20 '18 at 23:28
  • 1
    @AndrewThompson I was just waiting for that to blow up in there face ... but I'm mean like that – MadProgrammer Mar 20 '18 at 23:35

1 Answers1

0

If you want to simulate the click of the "X" then dispatch a windowEvent(closing in this case) to the "Yuval Library".

jframe.dispatchEvent(new WindowEvent(jframe, WindowEvent.WINDOW_CLOSING));

If you want simply close the JFrame dispose it:

jframe.dispose();
Overflow 404
  • 482
  • 5
  • 20