I've created a Swing application with several JInternalFrames which gets added to a JDesktopPane on the event of a mouse click. I want only one instance of the same Internal frame to be present on the DesktopPane. I dont want the same frame to appear twice when the user opens the frame..
-
2Show us anything that you have tried. – Kakalokia Mar 17 '13 at 13:01
-
I used this method "removeAll()" – pixylife Mar 17 '13 at 13:18
-
The idea is : if you are clicking on a Label or a Button use setEnable(false) this will disable the button until user closes the InternalFrame then Enable the button. – Azad Mar 17 '13 at 13:29
6 Answers
The simple solution to your problem is to create an HashMap<String,JInternalFrame>
. The key
will be the title of that JInternalFrame
and value
will be the object
of that JInternalframe
opened currently.Save the (key,value
) pair in HashMap
when the internal frame is opened first time. Disable the close button for all JInternalFrame
window , so that user can't dispose the displayed JInternalFrame
window. Register esc
key to each JInternalFrame
object , so that when esc
button of keyboard is pressed the currently display JInternalFrame
is minimized on the DesktopPane
.Now When you click on menu item
to open that same internal frame, check if the title
of that JInternalFrame
is existing in that HashMap
askey
. If it exists then retrieve the value
for that key
and refer it by JInternalFrame
variable and then restore the same on DesktopPane
. If the corresponding entry of title
doesn't exist in that HashMap
, create a new JInternalFrame
object, make an entry for same in the HasMap
and display it.
Note: Whatever I have posted here is the solution for the situation where you can have many types of
JInternalFrame
each having unique differentfunctionality
, and you want to keep only oneinstance
of each of thoseJInternalFrame
.

- 12,976
- 2
- 27
- 38
-
http://docs.oracle.com/javase/tutorial/uiswing/events/internalframelistener.html is this useful.. – pixylife Mar 17 '13 at 13:32
-
@luzifer: yeah.I had went through earlier..you can either hide or minimize the `JInternalFrame` depending upon your need.. What I have posted here is the solution for the situation where you can have many types of `JInternalFrame` each having unique different functionality, and you want to keep only one instance of each of those `JInternalFrame`.. – Vishal K Mar 17 '13 at 13:38
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;

- 71
- 5
Try this simple code :
YourJinternalFrame nw = new YourJinternalFrame();
private void YourJinternalFrameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
if(!nw.isVisible()){
YourJDesktopPane.add(nw);
nw.setVisible(true);
}
}

- 11
- 1
Try this simple code take class variable chk and set equal to 0 then call jframe method componentremoved in this set chk =0 again and if you call your internal frame set chk =1 and compare chk on calling internal wheather it is zero or not thats all
I went for a different solution:
final JInternalFrame[] frames = desktopPane.getAllFrames();
if( !Arrays.asList(frames).contains(loginFrame) ) {
loginFrame = new LoginFrame();
desktopPane.add(loginFrame);
loginFrame.setVisible(true);
}

- 159
- 1
- 3
This worked for me (checking for the class name):
final JInternalFrame[] frames = desktopPane.getAllFrames();
LoginFrame loginFrame = new LoginFrame();
if( !Arrays.asList(frames).toString().contains("LoginFrame") ) {
desktopPane.add(loginFrame);
loginFrame.setVisible(true);
loginFrame.validate();
}

- 1
- 1