-2

I am actually working on a java project which shows many frames, and when we click on a button then we go to the appropriate frame ... the problem is that the first frame always stays visible.

I have tried f1.setVisible(false); in the action performed, but the same problem persist.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
john carter
  • 53
  • 1
  • 1
  • 5
  • 1
    Please show us some code. We need to see how you are attempting to do the task in order to tell you what may be wrong. – Vincent Ramdhanie Dec 03 '12 at 14:42
  • 5
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) BTW - by *"project which shows many frames"* DYM `JFrame` instances, or frames in web pages,or video, or..? – Andrew Thompson Dec 03 '12 at 14:42
  • 3
    Your problem is that you're likely calling `setVisible(false)` on the wrong JFrame instance since if you called this on the actual visualized JFrame, it certainly would disappear. But having said this I heartily endorse @Andrew's and MRI's recommendations (1+ to both of them). Use a CardLayout. – Hovercraft Full Of Eels Dec 03 '12 at 14:53
  • I'm trying to apply the MVC architecture in eclipse there is too much code to paste in fact there are three classes model, view & controller & in the controller the treatment of action listener and action performed as shown in the following example: class b2listener implements ActionListener { public void actionPerformed(ActionEvent e) { f1.setVisible(false); Frame2 f2= new Frame2(); f2.setVisible(true); } } well you do not have to understand all that I want just a simple example when I click on a button the window mother will be gone and the new window is displayed ... – john carter Dec 03 '12 at 15:01

2 Answers2

6

I've suggested this like a thousand times, but instead of multiple frames, have a single frame use an appropriate layout manager (e.g. CardLayout) that can toggle multiple views.

For more information, see How to Use CardLayout.

mre
  • 43,520
  • 33
  • 120
  • 170
-1

Upon opening a new frame, also call a method which closes your window

private void closeWindow(){
    WindowEvent event = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • 2
    True, upon replacing that code with setVisible, it behaved as intended. Appearantly he flagged this as the solution, so I guess it worked for him somehow. Would be a lot easier to identify the problem if we had some code to look at. – Jeroen Vannevel Dec 03 '12 at 15:51