4

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?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3319146
  • 47
  • 1
  • 8
  • Check the JInternalFrame.addInternalFrameListener() method, InternalFrameListener.internalFrameClosing() and InternalFrameListener.internalFrameClosed() listener methods and corresponding tutorial at http://docs.oracle.com/javase/tutorial/uiswing/events/internalframelistener.html. – Oleg Estekhin Feb 26 '14 at 15:35
  • it direct to Error 404 page, but I will try your code. Thank you – user3319146 Feb 27 '14 at 11:03
  • Seems like oracle site has some strange rewrite/redirect checks in place, try to go to http://docs.oracle.com/javase/tutorial/uiswing/TOC.html and select the topic "How to Write an Internal Frame Listener" manually. – Oleg Estekhin Feb 27 '14 at 11:11

1 Answers1

8

"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.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720