File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Hiding a JFrame using a Submit button? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Hiding a JFrame using a Submit button?" Watch "Hiding a JFrame using a Submit button?" New topic
Author

Hiding a JFrame using a Submit button?

Jason Sgalla
Greenhorn

Joined: Mar 22, 2010
Posts: 10
I am currently working on a small program to freshen up on some introductory Java and prepare for my Java class coming up later in my Bach program. I chose to create a small program to keep track of data.

The program opens with a window displaying the current date and a calendar with selectable buttons for each day. When the button is clicked for that day, it opens up a new window. The new window is where the information is to be entered. The problem I am having is with the Submit button and trying to figure out how to make it close the window (or hide it or dispose it).

I have done some research but everything I found shows information about WindowListeners to show how the close button on the frames work and I don't think that will work with this particular scenario, unless I'm just not fully understanding it which is very possible.

Here is the code I have:

The underlined "frame" is where the problem is.

I think I understand why it isn't working, but I haven't been able to figure out how to make it work. I am new to Java and it has been seven months since my Java I class that didn't cover any Swing concepts so I'm a bit stuck. The program works fine at the moment but you have to hit the X button on the Frame to close/hide the frame after you hit the submit button. I have purchased Swing A Beginner's Guide by Herbert Schildt to help me out with the GUI programming but there isn't any mention of this issue in the book or implementation of it in any of the modules.

Any help would be appreciated. A link to a reference or perhaps some first hand experience you have with this issue would be great! I could put that cancel button back on the window. Until then I'll keep on keeping on .

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

First thought: have you actually added the ActionListener to the button? If not, pressing the button isn't going to do anything.
Jason Sgalla
Greenhorn

Joined: Mar 22, 2010
Posts: 10
Yeah, I added the ActionListener to the button. When I press the button I get a slew of errors because the problem on line 14.

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: frame cannot be resolved

I can't figure out how to get that object, frame, into the actionPerformed method to use the .setVisible() method.

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3366
    
    9
Make the frame an instance variable, then you can refer to it in the part where you initialize it, as well as any listeners.
Jason Sgalla
Greenhorn

Joined: Mar 22, 2010
Posts: 10
Stephan van Hulst wrote:Make the frame an instance variable, then you can refer to it in the part where you initialize it, as well as any listeners.


Thank you very much for your help! It is now working as intended and I have learned my lesson regarding declaring and initializing variables at the same time.

I'm at the beginning of a very long learning path. Thanks again for helping me make a step in the right direction .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hiding a JFrame using a Submit button?
 
Similar Threads
Question about empty frame
swing ques- JFrame
Facing Exception in a simple swing program
Creating and showing JFrame from another JFrame
How to close this window?