Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

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

0
sahanz
Asked:
sahanz
  • 2
  • 2
  • 2
  • +2
1 Solution
 
sahanzAuthor Commented:
I'm talking about java, not javascript
0
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
zzynxSoftware 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:
Thanks, got it worked
0
 
zzynxSoftware engineerCommented:
You're welcome.
Thanx 4 axxepting
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now