GUI Exit button

How do I close the Frame

Page 1 of 1

9 Replies - 14909 Views - Last Post: 29 July 2009 - 05:45 PM Rate Topic: -----

#1 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

GUI Exit button

Posted 29 July 2009 - 10:32 AM

Hello,

Small and extremely simple task, but wrecking my brains. I got a menu frame open that opens up another frame. Now how do I code the exit button to close that frame and go back to the menu frame without pressing the 'x' on the right side of the titlebar which in effects closes the entire program.
Is This A Good Question/Topic? 0
  • +

Replies To: GUI Exit button

#2 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: GUI Exit button

Posted 29 July 2009 - 10:58 AM

I didnt quite get it without seeing the code.
But you can do setVisible = false.
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: GUI Exit button

Posted 29 July 2009 - 10:58 AM

This might not be as easy a task as u think. If you use the standard dispose code, all might be dispoased. Have you got a JFrame opening up another JFrame or JPanel? What you might have to do is created a father/son relationship, but will discuss that with more details when we see what you have.
Was This Post Helpful? 0
  • +
  • -

#4 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Re: GUI Exit button

Posted 29 July 2009 - 11:15 AM

The thing is its done in Netbeans which means if I post the whole code you'll see a whole lot of confusing stuff so I'll just mention the necessary parts.

I got a menu frame called MainMenu, with an menu View and menuItem Books. When user clicks View -> Books, the Books JFrame would open up with MainMenu still open behind the Books jframe with the following code:

private void mnuViewBooksActionPerformed(java.awt.event.ActionEvent evt) {
	 Book books = new Book();
	 books.setVisible(true);
}


In book jframe, aside from the jlabels and jtextfield, I got four buttons, one of which is Exit. Now what I want to do is code the exit button so when the user clicks the exit button, the books jframe closes and the mainmenu jframe is open, to allow the user to choose something else on the menu.

So far, I've tried quite a few things, current I got this happening:

setDefaultCloseOperation(EXIT_ON_CLOSE);


It doesn't work.
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: GUI Exit button

Posted 29 July 2009 - 11:24 AM

Ok, this is what I done, but I was working with a JFrame and a JPanel in a father/son relationship. On my JFrame, when a print button was pressed, a new frame would show up. The code I used to do this was
	private void btnPrint_actionPerformed() 
		{ 		
		setVisible(false);
		JDialog dialog = new JDialog(this, "Print_Results", true);
		dialog.add(new PrintResults());
		dialog.pack();
		dialog.setVisible(true);
		this.dispose();

		} 


So basically it creates a dialog and places the new panel in it, and shows it in place of the JFrame etc. Then in my JPanel class, when the exit or back button was pushed, I wanted to get back to my JFrame. To do this, I added to my JPanel back action event
private void back_actionPerformed()  
	{
		SwingUtilities.getWindowAncestor(this).dispose();
		CreateAtlDatabase data = new CreateAtlDatabase();
	} 


What that does is such for the parent component of the JPanel, which is the JFrame, and places the JFrame back in its place. See if you can work somthing out from this.
Was This Post Helpful? 1
  • +
  • -

#6 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Re: GUI Exit button

Posted 29 July 2009 - 12:09 PM

Hi,

I've managed to get the main menu to disappear, which is not what I really wanted. Tried to implement similar code for the books jframe through the exit button actionperformed, but still not working.

EDIT:
Ok, got it working. What I did was remove the this.dispose() from my mainmenu, since I didn't really want that gone, I didn't mind it being on the background. And implemented the this.dispose() in my books jframe code, seems to work just as I wanted it. Thanks.

This post has been edited by ladyinblack: 29 July 2009 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: GUI Exit button

Posted 29 July 2009 - 02:08 PM

View Postladyinblack, on 29 Jul, 2009 - 01:09 PM, said:

Hi,

I've managed to get the main menu to disappear, which is not what I really wanted. Tried to implement similar code for the books jframe through the exit button actionperformed, but still not working.

EDIT:
Ok, got it working. What I did was remove the this.dispose() from my mainmenu, since I didn't really want that gone, I didn't mind it being on the background. And implemented the this.dispose() in my books jframe code, seems to work just as I wanted it. Thanks.


JButton exit = new JButton("Exit");
exit.addActionListener(/*class implementing action listener*/);

public void actionPerformed(ActionEvent e){
	 if(e.getSource() == exit) System.exit(0);
}



This is for everyone doing it the hard way.

This post has been edited by macosxnerd101: 29 July 2009 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: GUI Exit button

Posted 29 July 2009 - 02:32 PM

View Postmacosxnerd101, on 29 Jul, 2009 - 01:08 PM, said:

View Postladyinblack, on 29 Jul, 2009 - 01:09 PM, said:

Hi,

I've managed to get the main menu to disappear, which is not what I really wanted. Tried to implement similar code for the books jframe through the exit button actionperformed, but still not working.

EDIT:
Ok, got it working. What I did was remove the this.dispose() from my mainmenu, since I didn't really want that gone, I didn't mind it being on the background. And implemented the this.dispose() in my books jframe code, seems to work just as I wanted it. Thanks.


JButton exit = new JButton("Exit");
exit.addActionListener(/*class implementing action listener*/);

public void actionPerformed(ActionEvent e){
	 if(e.getSource() == exit) System.exit(0);
}



This is for everyone doing it the hard way.


That will exit out of the whole system though, which isnt what she wanted.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: GUI Exit button

Posted 29 July 2009 - 05:05 PM

Oh whoops.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GUI Exit button

Posted 29 July 2009 - 05:45 PM

public void actionPerformed(ActionEvent e){
	 if(e.getSource() == exit) {
		  this.dispose();
		  System.exit(0);
	 }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1