• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using dispose()  RSS feed

 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a GUI that I need to reset, and to do it I want to dispose of the original and create a new instance. The action is called from within the GUI itself (ie. there's a reset button that kills off its own window and creates a new one).

The problem is, I keep getting "dispose() cannot be called from within a static context" error. I've taken "static" out of the methods (inc. main), and can't find anything else that would be causing it.



Is this a good way to go about this, or should I try another way? If not, can anyone think what's wrong with this code?

I've not done any real Java for over a year, and I'm working on some existing code modifying an old program as part of a uni project, so expect to see me back here a fair bit as I try to sharpen up my coding

Edit: Would help if I actually asked a question...
[ March 02, 2006: Message edited by: John Brookes ]
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GUI is the name of the class. You have declared dispose() to be non-static so it has to be invoked on an instance of GUI, not on the class itself. Either declare dispose to be static or keep an instance of GUI around so you can dispose of it later.
 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I declare dispose() as static? It's a given method, not user-defined.

I tried calling dispose from within the GUI class (as opposed to having it called directly from the button event) by creating a method called "reset" but with similar errors:



 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, ok. You're writing "FORTRAN in Java". This is bad. Don't do this. "static" is to be used sparingly, never as a default, and never to "allow you to call methods from another class." If you're thinking this way, then there's a design problem, as here. I'll explain the solution below.

First of all, run through the program and delete the word "static" everywhere it appears, except in the declaration of main and that one static final int constant. This will make your members belong to a specific instance of the GUI class, rather than belonging to the class as a whole. It means that it will no longer be appropriate to EVER call GUI.anything(). But that's good -- that's what you want.

Now, give ButtonPanel a constructor argument of type GUI. When GUI creates a ButtonPanel, pass "this" as the constructor argument. In the constructor, store the parameter in a member variable of type GUI (which I'll call "myGUI", but you can name whatever you'd like.)

Now, everywhere in ButtonPanel you write "GUI", you need to write "myGUI", so you're referring to that one instance, instead of the class itself.

Now, finally, the good part: you'll find that whereas GUI.dispose() was illegal, myGUI.dispose() is not! You now have a reference to a class instance on which to call instance methods.
 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aah I think I've got you. I'll give it a try and see what happens.

Like I said, this isn't my code. I've been given someone elses program and been told to extend its functionality. Trying to figure out what he's done it a nightmare

Thanks for the help - I'll be back later with some results (or, more likely, more problems and questions!)
 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didn't like it when I took all the "static"s out - gave about 40 compiler errors - so I put them back in. I did everything else and it compiles now (with warnings) but doesn't run.

Warnings:

Note: C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\ConvoySimModel\InOutGUI.java uses or overrides a deprecated API.
Note: Recompile with -Xlint eprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.)


Terminal window error:
Exception in thread "main" java.lang.NoSuchMethodError: main



New code looks like this:



 
Matti Poro
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put static modifier back to the main-method. What kind of compilation errors did it give when you removed the statics?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Brookes:
It didn't like it when I took all the "static"s out - gave about 40 compiler errors - so I put them back in.


Right. Just taking out the statics would break everything. It won't start compiling again until you do the member variable thing, which you've done, so now the statics can, and should, come back out. As I did warn you that there were two "static"s that should stay, didn't I?
 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I swear I left them in! I didn't recompile it until I'd done everything you suggested (take out all unnecessary statics bar those two and sort out the member variable). I did just notice that I took it out of the "main" for some reason.

The compiler errors all read "non-static variable xxx cannot be referenced from a static context". This occurs once with ButtonPanel.java and plenty of times in VehicleAnimator.java.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, well, that means that somewhere you're calling a non-static method without specifying an object. We haven't seen the code for VehicleAnimator, but presunably it was like ButtonPanel and said GUI.whatever() all over the place. Presumably VehicleAnimator needs a myGUI member as well, and you need to do the same things to it that we did to ButtonPanel.

As far as the one remaining error in ButtonPanel, if you still don't see why there's an error, show us the file as it stand and indicate the exact line pointed to by the error message, and I'm sure we could help.
 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I've got you, and it's all sorted now. Every time I changed it for one class the error appeared in another, so I just went though all of them and added the object (search/replace is such a useful function). It works nicely too - the reset button is on the GUI when it runs, and when you press it the window closes and a new one opens.

The only problem is that now half my other buttons have stopped working. The problem isn't with those buttons in the "ButtonPanel" either, but to those on "ConfigPanel" and "PromptPanel", which makes me think it's an error in "GUI" somewhere, or something I've added to ButtonPanel hasn't been extended to the other two panels. I'll put one up as an example, in case yo ucan spot anything obviously wrong with it, along with the new code for GUI.

Note that there's only one button on PromptPanel - "Confirm".



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!