i have two questions concerning programming in Java.

1st Q:
i have more than frame opened for the same program and when i want to close one of them, all the program closes as well.
basically i write this code for closing the frame when clicking on a button:

JButton exit= new JButton("Exit");
exit.addActionListener( new ActionListener()
		   public void actionPerformed(ActionEvent e) 

2nd Q:
Why do the above addActionKistener method only deals with final variables ??

Thank you in advance

8 Years
Discussion Span
Last Post by Blackeagle


The System.exit() method terminates the Java Virtual Machine, not just a single frame.


Not sure what you mean by this Question.


Is there a way to close one frame only?

about Q2.
lets say i have a variable like this:
public int temp=1;

and then in one of the methods i use this code:

x.addActionListener( new ActionListener()
public void actionPerformed(ActionEvent e)

i'll get an error saying that temp should be final.

any reason why that happens ?


Is there a way to close one frame only?

Check out the dispose() method of the JFrame class.
You can also check this tutorial to learn more about Event Handling in Java.

The second question you mentioned is related to the concept of anonymous inner classes, but if you are beginning in Java then I suggest for now you stick to the method mentioned in The Java Tutorial which is my first link.

And please use [code] tags to post your code. Look here and here for more information of code tags

Votes + Comments
Well explained :)

i'll try that and Thank you all ..

sorry for not using codes in my post .. i'm still new to this forum.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.