I have 3 frames, Main (as the parent) and 2 JInternalFrames (F1 and F2 as children). When a button (inside parent) is pressed, I make an instance of F1 and send it as a parameter to ShowIntrlFrame() so F1 can be displayed inside frmContainter. My question here is, how can I open F2 from F1 and display it on frmContrainer (which is in the Main frame)?
public class Main extends javax.swing.JFrame{
public Main(){
}
private void btnOpenFrmActionPerformed(java.awt.event.ActionEvent evt) {
F1 f1 = new F1();
frmContainer(f1);
}
}
public void ShowIntrlFrame(JInternalFrame f){
f.setSize(1100, 620);
f.setLocation(0, 0);
f.setVisible(true);
frmContainer.removeAll();
frmContainer.add(f, BorderLayout.CENTER);
frmContainer.revalidate();
frmContainer.repaint();
}
What I would do is follow a delegation pattern via dependency injection.
This is, I would "delegate" the functionality need to generate and show the window to some other class and then "inject" that into the workflow as required
I'd start with a concept of a "window manager"...
At the moment, this is pretty basic and just opens a specified window. The nice thing about this, is we don't care if it's a
JInternalFrameorJFramewhich gets generated, that's not the callers responsibility.Next, we make a implementation which supports
JDesktopPaneNow, important to note, this class is acting as kind of factory, in that it's generating the content view and
JInternalFrame. I'd probably consider making a "content factory" which could be injected into this which would then create the content based on the desired destination, but that's probably getting a little more complicated then is required right now.Now, before you ask, the actual content is based on
JPanel, for example...Why? Because it's a
JPanelcan be added to any container and, generally, we don't care if it's aJInternalFrameorJFrame.And finally, some kind of "starting point"....
Runnable example...
But wait, there's more...
Currently, if you tap "Open" or "Show second" multiple times, you get a bunch of new windows. This may or may not be desirable, but could be easily fixed via the
WindowManager, in fact, you could create differentWindowManagers based on your needs, for example...