1

I want to dispose() my current jFrame and move to the next jFrame(StudentProfilepage()).But it shows error at this.dispose().

How can i do that.Here i used MouseListner a jLabel l1

My code as follows

 l1.setCursor(Cursor.getDefaultCursor());

        l1.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {

            //added check for MouseEvent.BUTTON1 which is left click
            if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {

                this.dispose();   //error here(i want to close my current frame and move to StudentProfile() page 

                new StudentProfilePage().setVisible(true);
            }
        }
    });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • What is your class name ? – Suresh Atta Aug 14 '13 at 15:19
  • 1
    Please have a look at [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), this suites better in this given situation. For [example](http://stackoverflow.com/a/9349137/1057230) :-) – nIcE cOw Aug 14 '13 at 15:44

2 Answers2

3

this in

this.dispose();

refers to the MouseAdapter, hence the compilation errors you are seeing.

You need to invoke dispose on the JFrame

JFrameClassName.this.dispose();

Also consider using a JDialog rather than a JFrame as the second window. Read The Use of Multiple JFrames, Good/Bad Practice?

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
3

You should write

YourClassName.this.dispose();

which points to your jframe.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307