Solved

closing internal frames

Posted on 2004-04-17
41
1,277 Views
Last Modified: 2013-11-23
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
Comment
Question by:Samooramad
  • 17
  • 13
  • 6
  • +2
41 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849338
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
 
LVL 4

Expert Comment

by:brunomsilva
ID: 10849414
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
 

Author Comment

by:Samooramad
ID: 10849433
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
 

Author Comment

by:Samooramad
ID: 10849442
CEHJ what do you mean by the returned array?
0
 
LVL 4

Expert Comment

by:brunomsilva
ID: 10849452
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849475
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
 
LVL 1

Expert Comment

by:RodionP
ID: 10849484
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
 
LVL 4

Expert Comment

by:brunomsilva
ID: 10849493
cehj,

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

bruno
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849495
Oh yes, i forgot about removing the close button - that is theoretically possible but certainly not straightforward
0
 

Author Comment

by:Samooramad
ID: 10849497
yes CEHJ
It was the only way i could get the internal frame to work.
0
 

Author Comment

by:Samooramad
ID: 10849499
so does that mean using dispose would be better?
0
 
LVL 4

Expert Comment

by:brunomsilva
ID: 10849511
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
 
LVL 4

Expert Comment

by:brunomsilva
ID: 10849527
using dispose will save you some typing and works for you purpose :) be sure to read the API also
0
 

Author Comment

by:Samooramad
ID: 10849536
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849537
>>...doesn't it hide the X button?

You could be right - i haven't tried it ;-)
0
 

Author Comment

by:Samooramad
ID: 10849543
bruno what do u mean by API?
sorry but i really meant it when i said break it down :)
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 10849552
Try

 JInternalFrame[] openFrames = desktopPane.getAllFrames();
0
 

Author Comment

by:Samooramad
ID: 10849585
CEHJ
>> desktopPane.getAllFrames(); is desktoppane the name of the
JDesktopPane I used? do i replace it?
0
 

Author Comment

by:Samooramad
ID: 10849597
also >>JInternalFrame[] openFrames
do i leave it as is? or do i change JInternalFrame?
0
 
LVL 4

Accepted Solution

by:
brunomsilva earned 200 total points
ID: 10849608
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Samooramad
ID: 10849633
ok bruno could you please answer my last question so I could try it out?
thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849657
>>
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
 

Author Comment

by:Samooramad
ID: 10849673
I have another question
what does pack do? is it better than setting a size? why?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849700
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
 

Author Comment

by:Samooramad
ID: 10849730
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 10849771
You're not mixing AWT and Swing gui components are you by any chance?
0
 

Expert Comment

by:sendres
ID: 10849780
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
 

Author Comment

by:Samooramad
ID: 10849782
i think all i have used are swing components. what would be an AWT component?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849801
>>. what would be an AWT component?

Roughly speaking, any component not beginning with J is likely to be an AWT one
0
 

Author Comment

by:Samooramad
ID: 10849806
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
 

Author Comment

by:Samooramad
ID: 10849820
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849834
Hmm. Not sure. I'd be interested to know what happens when the internal frames are empty of applets
0
 

Expert Comment

by:sendres
ID: 10849850
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
 

Author Comment

by:Samooramad
ID: 10849859
is there such a thing as JApplet? should i try that?
0
 

Author Comment

by:Samooramad
ID: 10849864
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
 

Assisted Solution

by:sendres
sendres earned 100 total points
ID: 10849874
Yes, there is. It's javax.swing.JApplet. You might try that instead.
0
 

Expert Comment

by:sendres
ID: 10849886
Applet is derived from java.awt.Panel, so you were using AWT components without realizing it!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849889
>>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
 

Author Comment

by:Samooramad
ID: 10849937
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849958
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10850023
8-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now