Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setVisible in JFrames?

Tags:

java

swing

jframe

In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).

Is there any special method(s) to do this? How can I do it?

like image 208
Jayanga Kaushalya Avatar asked Feb 27 '26 04:02

Jayanga Kaushalya


2 Answers

Consider using CardLayout instead of hunting for how many JFrames there are. Then..

  • only one JFrame would be needed
  • any of Next/Back Actions will be only switching between cards

There are lots of examples in this forum - e.g. as shown here.

like image 161
mKorbel Avatar answered Mar 01 '26 18:03

mKorbel


That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.

like image 39
Andrew Thompson Avatar answered Mar 01 '26 17:03

Andrew Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!