Reputation: 544
I have 1 MAIN-FRAME and 2 INTERNAL-FRAMES(say -> Frame 1
and Frame 2
) in my java netbeans project. To switch from the main-frame to Frame 1, I used the following code IN THE MAIN-FRAME'S CLASS :-
Frame1 frame = new Frame1();
frame.setVisible(true);
jDesktopPane1.add(frame);
setContentPane(jDesktopPane1);
The above code works perfectly fine, thereby creating the frame 1. But the below code WHICH IS IN THE FRAME 1's CLASS does not work unfortunately:-
Frame2 frame2 = new Frame2();
MainFrame mf = new MainFrame();
frame2.setVisible(true);
mf.setContentPane(frame2);
Please tell me where am I going wrong. I am kind of new to java. So, Please be calm if you find this question silly.
Upvotes: 0
Views: 860
Reputation: 5415
Since your didn't provide an SSCCE, I'll make some assumptions:
Frame1 and Frame2 are both JInternalFrames
MainFrame is a JFrame, and you only want one instance of it
Based on that, after Frame1 creates Frame2, it shouldn't create another MainFrame. Instead, Frame2 needs to be added to the JDesktopPane of the original MainFrame.
There are many ways to do this. One way would be to create a method to MainFrame that would allow callers to add a JInternalFrame to its desktop. Something like:
public void addFrame(JinternalFrame iFrame)
{
desktop.add(iFrame);
}
which would require each caller (Frame1 in this case) to have a handle to the original MainFrame instance.
Upvotes: 2