• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1478
  • Last Modified:

closing internal frames

Hi all
I have an application with a menu bar. depending on which menu item you select, a different applet will be displayed in the same internal frame. This is all working fine
my problem is now i want to remove the "X" button from the internal frame and add code so that when you select a menu item it checks first to see if another applet is currently open in the internal frame. if it is i need to close the internal frame without using the "X" button before opening the new applet so:

How do i check to see if an internal frame or applet are currently open?

and
how do i close the internal frame without the close button
thanks
0
Samooramad
Asked:
Samooramad
  • 17
  • 13
  • 6
  • +2
4 Solutions
 
CEHJCommented:
See

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JDesktopPane.html#getAllFrames()

Check the returned array. If its size > 0 then you can call dispose/setVisible(false) on any live references
0
 
brunomsilvaCommented:
to close an internal frame:

iframe.setClosed(true);

don't forget to catch the exception.

API:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JInternalFrame.html#setClosed(boolean)

bruno
0
 
SamooramadAuthor Commented:
how do i catch the exception
Guys please break this down as much as you can I'm new to this :)
I feel lost
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SamooramadAuthor Commented:
CEHJ what do you mean by the returned array?
0
 
brunomsilvaCommented:
try {
    iframe.setClosed(true); // iframe = name of your internal frame variable
} catch (PropertyVetoException e) { // if it can't be closed
    System.out.println("Unable to close frame!");
}

bruno
0
 
CEHJCommented:
You don't need to catch exceptions if you call dispose - it does the same as setClosed

>>CEHJ what do you mean by the returned array?

Are you using a JDesktopPane?
0
 
RodionPCommented:
Well as far as I understand you want to completely remove X button noy only to disable it, if you plan to do so, you should explore the inharitance tree, as far as I understand, everything that is displayable in Swing inherits from JComponent, and that is simply a rectangle, displayable rectangle, and there are number of classes that are between JComponent and JFrame, you should choose how far you want to go for that X button to disapear. Please note that since you generalizing your application to such abstraction you will/might have to implement all the functionality that is necessary to run it properly. Basically what I am saying is that you don't have to use JFrame, you could use JPanel or JWindow, I am not sure about specifics, take a look at the API
0
 
brunomsilvaCommented:
cehj,

i thought i had forgot about something, haven't used internal frames for a while though :)

bruno
0
 
CEHJCommented:
Oh yes, i forgot about removing the close button - that is theoretically possible but certainly not straightforward
0
 
SamooramadAuthor Commented:
yes CEHJ
It was the only way i could get the internal frame to work.
0
 
SamooramadAuthor Commented:
so does that mean using dispose would be better?
0
 
brunomsilvaCommented:
hmm, if you use the default java look and feel and use setClosable(false) or this constructor http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JInternalFrame.html#JInternalFrame(java.lang.String, boolean) , doesn't it hide the X button?
0
 
brunomsilvaCommented:
using dispose will save you some typing and works for you purpose :) be sure to read the API also
0
 
SamooramadAuthor Commented:
RodionP I would rather keep it in JFrame since that part is working now.

ok I will try dispose but how do i check if an internal frame is open or not?
0
 
CEHJCommented:
>>...doesn't it hide the X button?

You could be right - i haven't tried it ;-)
0
 
SamooramadAuthor Commented:
bruno what do u mean by API?
sorry but i really meant it when i said break it down :)
0
 
CEHJCommented:
Try

 JInternalFrame[] openFrames = desktopPane.getAllFrames();
0
 
SamooramadAuthor Commented:
CEHJ
>> desktopPane.getAllFrames(); is desktoppane the name of the
JDesktopPane I used? do i replace it?
0
 
SamooramadAuthor Commented:
also >>JInternalFrame[] openFrames
do i leave it as is? or do i change JInternalFrame?
0
 
brunomsilvaCommented:
try something like:

JInternalFrame[] openFrames = desktopPane.getAllFrames();
for (int i = 0; i < openFrames.length; i++) {
    openFrames[i].dispose();
}

this will close all open frames (which should be only one, i guess)
0
 
SamooramadAuthor Commented:
ok bruno could you please answer my last question so I could try it out?
thanks
0
 
CEHJCommented:
>>
is desktoppane the name of the
JDesktopPane I used? do i replace it?
>>

Yes


>>
also >>JInternalFrame[] openFrames
do i leave it as is?
>>

You can do
0
 
SamooramadAuthor Commented:
I have another question
what does pack do? is it better than setting a size? why?
0
 
CEHJCommented:
It just sizes the window using the preferred sizes of the contained components, effectively making just as big as it needs to be. It's a different way of approaching things - you may want it bigger than that
0
 
SamooramadAuthor Commented:
you guys rock!
the code you wrote worked, bruno!
I have yet another question:
the internal frame that contains the applet is placed in the center of the layout so it fits right underneath the menu bar
the problem is when i open the internal frame then need to select something from the menu bar, the drop down menus don't appear anymore. actually they seem to be getting diplayed behind the applet( or internal frame)
any ideas?
0
 
CEHJCommented:
You're not mixing AWT and Swing gui components are you by any chance?
0
 
sendresCommented:
Let's see if I can break this down a bit more for you.

You want to use a menu command to close an internal frame. The reason you use internal frames is because you want to have multiple frames open at once. So let's start with the assumption that you've written these internal frames as a separate class that extends JInternalFrame, like this:

public class MyClass extends JInternalFrame implements someThingOrOther {
    public MyClass() {
        // constructor stuff
    }

    public void myNifty method() {
        // nifty stuff here
    }

    public void someThingOrOther () {
        // more excellent work here
    }
}

Okay so far? Now, let's further presume you have a "File" menu with a "Close" menu item, and you want the currently selected internal frame to close when this menu item is selected. You've probably already set up you menu something like this:

        JMenuItem fileMenuCloseItem = new JMenuItem();
        fileMenuCloseItem.setText("Close");
        fileMenuCloseItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
                InputEvent.CTRL_MASK));
        fileMenuCloseItem.setActionCommand("close");
        fileMenuCloseItem.setMnemonic(KeyEvent.VK_C);
        fileMenuCloseItem.addActionListener(this);

        JMenu fileMenu = new JMenu();
        fileMenu.setText("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        // add other file menu items here, if needed
        fileMenu.add(fileMenuCloseItem);
        // add other file menu items here, if needed

        JMenuBar mainMenuBar = new JMenuBar();
        mainMenuBar.add(fileMenu);
        // add other menus here, if needed
        setJMenuBar(mainMenuBar);

So now we have a menu bar with a file menu and a close item on the file menu. We now need to listen for the user to choose a menu item. Presumably you have many menu items to listen for. I like to combine these all together into one listener to keep things organized. Assuming you have a file menu with items like 'New,' 'Open,' 'Import,' 'Export,' 'Close,' and 'Exit,' you can do something like this:

    public void actionPerformed(ActionEvent e) {
        if ("new".equals(e.getActionCommand())) {
            fileMenuNewItemActionPerformed();
        } else if ("open".equals(e.getActionCommand())) {
            fileMenuOpenItemActionPerformed();
        } else if ("import".equals(e.getActionCommand())) {
            fileMenuImportItemActionPerformed();
        } else if ("export".equals(e.getActionCommand())) {
            fileMenuExportItemActionPerformed();
        } else if ("close".equals(e.getActionCommand())) {
            fileMenuCloseItemActionPerformed();
        } else if ("exit".equals(e.getActionCommand())) {
            fileMenuExitItemActionPerformed();
        } else {
            mainExit();
        }
    }

So now a specific method will be called for each menu item. When the File-->Close item is selected we need to figure out which frame is open, then get rid of it:

    private void fileMenuCloseItemActionPerformed() {
        JInternalFrame imageToClose = desktopPane.getSelectedFrame();
        if (imageToClose != null) {
            imageToClose.dispose();
        }
    }

A couple of notes here. First, since we're talking about internal frames here, you must have a JDesktopPane to hold the internal frames. Here, I've assumed yours is called desktopPane because of your code sample above. Second, we get the selected frame on the desktop. If there's no selected frame on the desktop, getSelectedFrame will return null. So we check that imageToClose is not null, that there is a frame to close, then we dispose of it. I agree that using dispose is the easiest way to get rid of the internal frame. Dispose makes the internal frame invisible, unselected, and closed, all in one fell swoop!

I'll have to look at making an internal frame without a close button. I don't think I've ever needed to do that! Hope this helps...
0
 
SamooramadAuthor Commented:
i think all i have used are swing components. what would be an AWT component?
0
 
CEHJCommented:
>>. what would be an AWT component?

Roughly speaking, any component not beginning with J is likely to be an AWT one
0
 
SamooramadAuthor Commented:
dear sendres
bruno and cehj have already solved the first question for me...
if you have any idea how I could solve the last problem where i mentioned that my "applet inside an internal frame" is hiding my "dropdown menu items"
i would be very greatfull
thanks for the help tho :)
0
 
SamooramadAuthor Commented:
ok I looked through all my declarations and everything I have begins with a J except the declerations of Applet, Border and BorderLayout
could one of them be causing the problem?
0
 
CEHJCommented:
Hmm. Not sure. I'd be interested to know what happens when the internal frames are empty of applets
0
 
sendresCommented:
I'm guessing it has something to do with the order in which the components are painted to the screen. Because an applet, by definition, calls the init(), start(), and paint() methods in that order upon launch, perhaps the applet is being repainted after the menu is selected!?

Just a thought...
0
 
SamooramadAuthor Commented:
is there such a thing as JApplet? should i try that?
0
 
SamooramadAuthor Commented:
cehj i think your on to something. I tried removing the appletand just opening
an empty internal frame the problem went away. so i guess the applet is the problem. any solutions?
0
 
sendresCommented:
Yes, there is. It's javax.swing.JApplet. You might try that instead.
0
 
sendresCommented:
Applet is derived from java.awt.Panel, so you were using AWT components without realizing it!
0
 
CEHJCommented:
>>I tried removing the appletand just opening an empty internal frame the problem went away

If you can alter the applets, try making them JApplet
0
 
SamooramadAuthor Commented:
I guess that is the problem but when i tried changing it from Applet to JApplet i got an error:

"Error #: 354 : incompatible types; found: visualizer.Spots, required: javax.swing.JApplet at line 72, column 28

but i already added that in import
0
 
CEHJCommented:
Yes,, but you've probably got declarations elsewhere that need changing - it's not enough just to change the type in the class declaration
0
 
CEHJCommented:
8-)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 17
  • 13
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now