Close a window on button click

This code is wrote for click event.When click the button main window appeared.but window 1 is not closed.button is in the window1.I want to
close the window 1 when click the button..
if(no==25){
                JOptionPane.showMessageDialog(null,"Number correct");
		mainwindow mw=new mainwindow();
		mw.show();
		window1 w1=new window1();
		w1.dispose();
		}

Open in new window

LVL 1
sahanzAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
zzynxConnect With a Mentor Software engineerCommented:
>> button is in the window1

Then in the code of class window1 you should have somthing like:


JButton yourButton = new JButton();
...
yourButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          closeWindow();
      }
});

with

private void closeWindow() {
    this.dispose();
}
0
 
sahanzAuthor Commented:
I'm talking about java, not javascript
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Gurvinder Pal SinghCommented:
Sorry...lolz

Is mainwindow a frame?
have you tried this?
http://stackoverflow.com/questions/573317/java-swing-close-window-without-exiting-app
0
 
hazgodukCommented:
You're creating a new frame then closing it which doesn't seem right. If your button is in the frame you want to close why are you creating a new one? You want to just do

dispose();

0
 
ksivananthCommented:
if you want to close the existing window, you shouldn't create a new instance instead should call the dispose method in the existing instance!
0
 
sahanzAuthor Commented:
Thanks, got it worked
0
 
zzynxSoftware engineerCommented:
You're welcome.
Thanx 4 axxepting
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.