-4

I'm trying to open a JFrame which is in class B by clicking on a button which is present in Class A. I'm using JFrameForm in the project. How would I achieve this?

I tried using this code,it successfully opens the JFrame in Class B (JDBC_Trial) but doesn't close the existing one in class A

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JDBC_Trial().setVisible(true);
} }); // TODO add your handling code here: }
Manu
  • 51
  • 5
  • I suggest not using more than 1 JFrame. It's considered bad practice. Instead, try using a [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Jonah Dec 26 '15 at 16:56
  • I also suggest posting some of the code that you have tried. We want to **help**, so you can **learn** not just do everything for you – Jonah Dec 26 '15 at 17:02
  • Thank you for the suggestion.This i my first time in Stackoverflow.Sorry. – Manu Dec 26 '15 at 18:05
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Dec 27 '15 at 05:18

2 Answers2

0

Using a dispatch event allows you to choose what happens to the frame. Here I've closed it:

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
JDBC_Trial p = new JDBC_Trial();
p.setVisible(true);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-1

Its not recommended, but it is possible:

Download download = new Download();
download.setDefaultCloseOperation(download.HIDE_ON_CLOSE);
download.setVisible(true);

Just put that on the event you want to call it on.