Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes How to dispose a jFrame using a timer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to dispose a jFrame using a timer?" Watch "How to dispose a jFrame using a timer?" New topic
Author

How to dispose a jFrame using a timer?

Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
Hi all! I'm using NB and want to dispose the main jFrame(which appears first when a program is run) via a timer. Please tell me the syntax in detail(where to write the code etc) TY.


AAL... IZZ... WELLL...
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Hello Ankit and welcome to the Ranch!

Have you read the API for JFrame (not jFrame)?

Have you read the API for javax.swing.Timer?

Have you gone through the tutorials linked from those APIs?

Please tell me the syntax in detail(where to write the code etc)

Um, no. That would deprive you of a learning opportunity.


luck, db
There are no new questions, but there may be new answers.
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
No. The timer isn't in my course but i want to make one. And i've tried many codes i could find on the net but i always get loads of errors. I think that i write the code in the wrong place.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
I always thought the Java Tutorial about timers was easy to read and follow.
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
Ok, so if i write the codes gn in the link(s), will it get activated as soon as i press shift+F6? or will it need an event other than run project?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
What does shift‑f6 do? Do you have a Listener for that combination?
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
In netbeans, shift+f6 is used to run your project. So, i was asking that will the timer need an event other than shift+f6 to activate if yes, what? Currently i'm trying to use the window activated event. Just experimenting. ;)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Ankit, at your level of learning you really need to drop the IDE and learn how to compile and run a Java program from the command prompt. NetBeans has hindered that learning process to the extent that you aren't aware what's a part of Jav aand what's a part of NetBeans.

All the Shift-F6 combination does is launch the main(...) method of the class you have defined in the Project Properties to be the Main class, in whatever is currently NetBeans' Main Project. What Timers or other classes are constructed and what methods are invoked depends on the code and program flow initiated from that main(...) method, and nothing else.
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
Ok, but i've always studied java using netbeans and this is my last year in school so i think i must go on with this a little more. and I think that the actual generated code must be present in the "Generated code" section in the source code.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Ankit Kashyap wrote:Ok, but i've always studied java using netbeans and this is my last year in school so i think i must go on with this a little more.

Bad move, in my opinion, because you'll only have to do it later. Basically, you're missing an important part of your "Java education".

and I think that the actual generated code must be present in the "Generated code" section in the source code.

Then all I can suggest is that you pull it up and look at it.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
What should be the event generated when a project is run(when the main JFrame appears)?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Ankit Kashyap wrote:What should be the event generated when a project is run(when the main JFrame appears)?


Haven't I already answered that?
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
O.o where? I tried window activated event but that didn't help. EDIT: Yeah, i saw your post but,.. can you explain it a bit more, preferably with an example?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Ankit Kashyap wrote:O.o where? I tried window activated event but that didn't help. EDIT: Yeah, i saw your post but,.. can you explain it a bit more, preferably with an example?

Darryl Burke wrote:All the Shift-F6 combination does is launch the main(...) method of the class you have defined in the Project Properties to be the Main class, in whatever is currently NetBeans' Main Project.

Code example:


Steve
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
So the main method is the event/initiator here. When i write the timer code i have to use this as the trigger?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Ankit Kashyap wrote:So the main method is the event/initiator here. When i write the timer code i have to use this as the trigger?

It's not an event, it is the entry point. It corresponds to "What should be the event generated when a project is run". There is no real event when a project is run, since Java does not know or care about projects, it just knows about the entry point of the application. Which is main. If you don't understand the difference it enforce's Winston's and Daryl's advise to stop using the IDE - it prevents you from actually understanding what is going on. And that will have bad affects later on (like when you make an application you want to give to someone else who is not a programmer.)

Whether main() is the appropriate place to start the Timer or not depends on the code and how it is setup. If you have a custom extension of JFrame, I would consider starting it inside the constructor. Otherwise I would consider starting it around the code where you create and display the JFrame. And if you want to use an event (for example if the JFrame can be opened multiple times through the course of the application) then you should look at JFrame's API to see which listeners and events make most sense to use.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

... and to write Swing code that will work consistently, you need to be aware of the issues surrounding Concurrency in Swing.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Darryl Burke wrote:All the Shift-F6 combination does is launch the main(...) method of the class you have defined in the Project Properties to be the Main class, in whatever is currently NetBeans' Main Project.


A correction: F6 does that. Shift-F6 launches the main(...) method of the class currently shown in the selected tab of the code editor.
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
I want to use it as a loading screen, much like a splash screen. The frame will be visible only once. So i should write the timer code in the main method where you have written the code for text output?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
Moving to our GUIs forum.

If you want a splash screen (do you really want a splash screen?) consider a dialogue.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Ankit Kashyap wrote:I want to use it as a loading screen, much like a splash screen.

So use a splash screen: How to Create a Splash Screen
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
I said 'much like' splash screen. Anyways it is possible to make one using undecorated JFrame.
Ankit Kashyap
Greenhorn

Joined: Jan 08, 2013
Posts: 13
Oh, i'm back. I tried what you said but, i only got loads of errors. Only one time did i create a timer but of no use. That just opened up a new, empty and tiny JFrame every 5 seconds. I messed up with my project so badly that, it did not worked correctly afterwards. Then i opened up a new project, copied all the swing components(That's what i had at the time of messing up my project. Because i first want my main JFrame to work "like" a splash/loading screen. And without it i can't continue over to the rest of the project.) to the new project.

Now i'm posting a screenie of my project showing the main JFrame(which needs to be timed), the main class, some properties etc.(please see carefully)

Please explain me what needs to be done and how? Where should i write the code(please explain the important lines of the code. please) and the syntax of the timer. And please attach the code so that it can be applied in netbeans, as netbeans does not allow to edit the generated code.

Thank you.



[Thumbnail for Problem.PNG]

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Since your goal is "much like a splash screen" maybe you should start from the premise that it is a splash screen and tell us what you would like to do differently.

Alternatively, you can show us the code that you got and the errors that you got, and we can try to tell you what is wrong. If you do this, I suggest you start from scratch with the bare minimum code (See SSCCE <- link). And forget your IDE.

But I think asking us to hand-hold you through the development is a little much to ask from a volunteer forum. Of course, you could also ask about hiring a consultant who could help you with the application, You will probably find someone willing to get paid to dedicate the time required to do this for you.
 
Don't get me started about those stupid light bulbs.
 
subject: How to dispose a jFrame using a timer?
 
Similar Threads
Closing a JFrame
Close Parent Window When Child Window Closes
Closing a JFrame
How to end a dialog window?
destroying an applet