I'm trying to create a JButton in 1 of my 2 JFrames which exits the top or current JFrame...
If I try:

System.exit(0);

The entire, currently running Java Virtual Machine, exits... (both frames)

I'm basically trying to get the same result as

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

does to the X button on the JFrame, onto another JButton...


Any way to do that?

You'd need to create a button that has an actionListener that, upon activation, will access a reference to whichever pane is "focused" then you'll need to access that pane's window-hiding command (you'll have to pick the command that hides the window permanently to emulate a close).

The way I have it set up:
JFrame / JPanel - JButton
\ JLabel - some painted text

In the JButton's actionListener:
getRootPane().getParent().setVisible(false);
seems to hide the frame.

It doesn't dispose of it though which means its still hanging there in memory right? No garbage collection?

I guess it's not a big deal though... Not much in there... Thnx.

ohhh.. I think I know why I didn't notice dispose() as a function...

public class Testing extends JFrame{
	
	public static void main(String[] args){
		Testing test = new Testing();
	}
	
	public Testing(){
		JButton ok = new JButton("OK");
		ok.setMargin(new Insets(0,5,0,5));
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				this.dispose();
			}
		});
		this.add(ok);
		this.pack();
		this.setVisible(true);
	}
}

I've had this problem before and don't know why this happens... When I try to use this.something inside actionPerformed(), it doesn't seem to work..... dispose() without "this" works perfect though..... Shouldn't "this.something" bring me back to the extended JFrame???

Probably a stupid question, but I'm learning all this GUI stuff by myself, without any help except from you guys...


Also, can someone explain to me how getContentPane() and getParent() are different?
If I were working on a JPanel for example, and I call getContentPane(), it would return the JPanel right? And if I were working on a JButton inside a JPanel, JButton.getParent() would return the JPanel right? What about JButton.getContentPane()? The pane inside the JButton?

I'm never actually sure what I'm doing and end up using trial and error without actually knowing if I'm doing the right thing or if there is an easier way...

"this" refers to the enclosing instance, which in your case above is an anonymous inner class that implements ActionListener. It's not a JFrame. If you drop the "this", or qualify is with "Test.this" it would work fine.

ohhh... so what I just did was ActionListener.this.dispose() which obviously doesn't exist...

When I did:

.....(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				ActionListener.this.
			}
		});

I was no longer in the outer class, rather in the inner class which "this" now refers too..

Thanks Ezzaral.. This helps me a lot...

"this" already refers to that inner class instance. You only need to qualify it if you want to refer to the enclosing class instance, like "Testing.this".

This article has been dead for over six months. Start a new discussion instead.