ProgramME
ProgramME

Reputation: 653

How to destroy the previous frame open opening a new frame

I am building an application in java that has 2 JFrames. 1st frame has a button that upon clicking should open the next JFrame while the previous one should close. I know how to display the next frame but: How do I close the previous JFrame automatically when the next JFrame opens ?

I tried thie following code:

addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new GUI();//next frame
                                this.dispose();//compile-time-error
            }

Upvotes: 3

Views: 6280

Answers (1)

Ilya Saunkin
Ilya Saunkin

Reputation: 19820

Where you call this.dispose(), the object referred by this is an instance of ActionListener. To invoke the instance of GUI type GUI.this.dispose() instead.

Upvotes: 8

Related Questions