Reputation: 765
I am working on a swing application using JinternalFrames but when i open one Jinternelframe it appears in the JDesktopePane and with another click on the same component another instance show up. i tried to fix this problem by declaring new instance of each JInternalFrame in the Constructor, but in the memory side it's useless, so i am asking if there is any methode to get rid of this issue. Thank you a lot guys.
Upvotes: 0
Views: 786
Reputation: 71
Here is may sample code. hope this help. Menu action to call internal frame in main application where JdesktopPane in it.
private void YourJinternalFrameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
YourJinternalFrame nw = YourJinternalFrame.getInstance();
nw.pack();
//usefull part for you.. if open shows, if not creates new one
if (nw.isVisible()) {
} else {
desktopPane.add(nw);
nw.setVisible(true);
}
try {
nw.setMaximum(true);
} catch (PropertyVetoException ex) {
Logger.getLogger(MainApplication.class.getName()).log(Level.SEVERE, null, ex);
}
}
put this inside of your YourJinternalFrame
private static YourJinternalFrame myInstance;
public static YourJinternalFrame getInstance() {
if (myInstance == null) {
myInstance = new YourJinternalFrame();
}
return myInstance;
Upvotes: 0
Reputation: 691893
Lazy-init the frames:
private JInternalFrame frame1;
private JInternalFrame frame2;
...
/**
* invoked when the button used to show the first frame is clicked
*/
private void showFrame1() {
if (frame1 == null) {
frame1 = new JInternalFrame();
// TODO initialize the frame
}
// TODO show the frame
}
// same for the other frames
Upvotes: 2