Log in

View Full Version : Begnner Java Question!


mirkazemisaman
01-14-2004, 01:34 PM
Hi everyone,
I am posting this here after not getting a responce in a "programming forum". I, however always get help here.

Anyways, it is very simple.

I have a program (Break Out) running in JFrame with a Thread (Runnable) etc and I want to make a method that when called resets the whole program, meaning that it disposes the current window and then calls the main method to start a fresh window of the application. How do I go about doing this?

I need this for this game I have written, and I have tried resetting all my variables when the user clacks "replay" at the end to start a new game, but somehow things just don't function properly and I need to just start a fresh new window... is this even possible (calling the main() of a class from within the class and disposing the current window and replacing it by a new one??)

Any help will be greatly appreciated.

Janak Parekh
01-14-2004, 04:54 PM
How do I go about doing this?
JFrame has a method called dispose() which will destroy the window itself, without having to wait for garbage collection.

As for the main() method, yes, you should be able to call it from anywhere. However, that's considered bad form. I'd suggest you have main() call another method, such as "initialize()", and recall that method if you want to.

That said, there should be a way of "fixing" your problem without doing a dispose()/new JFrame().

--janak

mirkazemisaman
01-16-2004, 03:59 AM
Thanx Janak,
That worked, actually I found out that my problem was a very simple one and I can't belive how dumb I must have been to miss it after looking and editting my code for almost half an hour to try to get it to work.

I had initially declared the "main" with an String[] args argument and upon calling it I didn't realize how main has an argument so I came up with the conclusion that I can not call main from within the class containing it (which didn't seem right).

So I just removed the argument in the "main" declaration (since I wan't using it anyways) and was able to easily call it.

Another question though, this might be impossible as I saw a couple of questions posted regarding this in the Sun Java forums with no answers:

How do I keep a JFrame "always active", I know how to make it "always on top", but "always on top" deactivates the Frame for a split second before making it active again and usually doesn't work right all the time.

I need the Frame sort of like some Windows windows that stay on top AND active (ie: any clicks other than on the box gives you a beep) until you click OK, cancel etc. This is, however not a dialogue box. It is a JFrame with Graphics.

Janak Parekh
01-16-2004, 05:28 AM
So I just removed the argument in the "main" declaration (since I wan't using it anyways) and was able to easily call it.
But the JVM won't run a main method without the args parameter, I believe.

How do I keep a JFrame "always active", I know how to make it "always on top", but "always on top" deactivates the Frame for a split second before making it active again and usually doesn't work right all the time.
I think you're talking about modality. A "modal dialog" is one that requires the user to answer it before switching to other dialogs within the same application. I usually use JOptionPanes to do that; not sure if JFrames support it.

And as for a window that actively prevents you from switching to other applications, I have no idea if that's even doable in Java at all. The vast majority of Windows apps don't do this in the first place.

Sorry I can't give you more concrete advice on this one. :( You might want to try a few 'Net searches...

--janak

mirkazemisaman
01-16-2004, 08:41 PM
But the JVM won't run a main method without the args parameter, I believe.


True, but it is possible with BlueJ (free editor, compiler and debugger... very fast) which is what I use.

Janak Parekh
01-16-2004, 09:27 PM
True, but it is possible with BlueJ (free editor, compiler and debugger... very fast) which is what I use.
Yes, but are you going to redistribute your code? I assume you're not going to redistribute BlueJ along with it...

BTW, when your code gets larger, try a free full-blown Java IDE like Eclipse or NetBeans. They're a lot more complicated than BlueJ, but they're amazingly powerful. NetBeans has a built-in Swing graphical editor, and I think you can download one for Eclipse...

--janak

Korlon
01-20-2004, 07:11 PM
But the JVM won't run a main method without the args parameter, I believe.



Absolutely correct. public static void main() and public static void main(String[]) are two entirely different methods. Java.exe calls main(String[]).



How do I keep a JFrame "always active", I know how to make it "always on top", but "always on top" deactivates the Frame for a split second before making it active again and usually doesn't work right all the time.
I think you're talking about modality. A "modal dialog" is one that requires the user to answer it before switching to other dialogs within the same application. I usually use JOptionPanes to do that; not sure if JFrames support it.


There's no way to make your whole application modal and disable access to other windows. I am interested to know why you want to do so. Or perhaps I am not understanding your intent?

Janak Parekh
01-20-2004, 09:31 PM
Java.exe calls main(String[]).
Absolutely, and a quick check verifies that it doesn't look for any other main() methods.

There's no way to make your whole application modal and disable access to other windows. I am interested to know why you want to do so. Or perhaps I am not understanding your intent?
Some Windows applications *do* take control of the screen, but as you state, I don't think the JVM can... at least not in any obvious fashion I know of.

--janak

Korlon
01-20-2004, 09:48 PM
Some Windows applications *do* take control of the screen

Yesss, and we hatesss them. Nasssty, evil applicationsssesss.