Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hiding a JFrame using a Submit button?

 
Jason Sgalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6320
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic