• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to dispose a jFrame using a timer?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ankit Kashyap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always thought the Java Tutorial about timers was easy to read and follow.
 
Ankit Kashyap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does shift‑f6 do? Do you have a Listener for that combination?
 
Ankit Kashyap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ankit Kashyap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What should be the event generated when a project is run(when the main JFrame appears)?
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Ankit Kashyap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and to write Swing code that will work consistently, you need to be aware of the issues surrounding Concurrency in Swing.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our GUIs forum.

If you want a splash screen (do you really want a splash screen?) consider a dialogue.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I said 'much like' splash screen. Anyways it is possible to make one using undecorated JFrame.
 
Ankit Kashyap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Problem.PNG
[Thumbnail for Problem.PNG]
A screenie from my project.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic