Solved

Close a window on button click

Posted on 2010-09-07
8
647 Views
Last Modified: 2013-12-14
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
Comment
Question by:sahanz
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33619712
0
 
LVL 1

Author Comment

by:sahanz
ID: 33619737
I'm talking about java, not javascript
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33619769
Sorry...lolz

Is mainwindow a frame?
have you tried this?
http://stackoverflow.com/questions/573317/java-swing-close-window-without-exiting-app
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:hazgoduk
ID: 33623104
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 33624771
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
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 33634487
>> 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
 
LVL 1

Author Closing Comment

by:sahanz
ID: 33656636
Thanks, got it worked
0
 
LVL 37

Expert Comment

by:zzynx
ID: 33669747
You're welcome.
Thanx 4 axxepting
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question