Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do something when Close (X) button is clicked in JInternalFrame

I got a assignment to create a JInternalFrame that can call mutiple of my applet work (last assignment call SpherePainter) and can count how many 'applets' are running inside the JinternalFrame.

[the applet are running inside another JInternalFrame]

public class MyInternalFrame extends JInternalFrame {
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;

public MyInternalFrame() {
    super("SpherePainter #" + (++openFrameCount), 
          true, 
          true, 
          true, 
          true);

    setSize(500,500);
    //Set the window's location.
    setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    SpherePainterApplet sp = new SpherePainterApplet ();
    sp.init();
    this.add(sp);

}

}

I can call the applet with no problem, but I cannot count how many of them are 'actually' running.

My method is to checkApp++ when one applet is create and checkApp-- when one is closed. Is there a way to use listener of closing button (X) or similar?

like image 864
user3319146 Avatar asked Feb 13 '23 16:02

user3319146


1 Answers

"Is there a way to use listener of closing button (X) or similar????"

The appropriate answer seems to be to use an InternalFrameListener as Oleg pointed out in the comment. You can see more at How to write InternalFrameListeners

public class IFrame extends JInternalFrame {

    public IFrame() {
        addInternalFrameListener(new InternalFrameAdapter(){
            public void internalFrameClosing(InternalFrameEvent e) {
                // do something  
            }
        });
    } 
}

Side-Note

"I got a assignment to create a JInternalFrame that can call mutiple of my applet work"

Whoever gave you this assignment, tell them to read Why CS teachers should stop teaching Java applets and possibly even The Use of Multiple JFrames, Good/Bad Practice?. IMO it seems like a useless nonsense assignment, if the requirements are how you described.

like image 137
Paul Samsotha Avatar answered May 10 '23 08:05

Paul Samsotha