Java - Halting a Program until an action is performed.
Howdy,
I have a program that has the following breakdown: cCity - base unit. Has the properties of name, latitude and longitude. These properties are set using a class cCityEditor ( extends JFrame, implements ActionListener ) which is called in the cCity's constructor. cWorld - holds a bunch of cities. The catch is, each city has to have a unique name, so whenever the addcity method is called, it runs through a check to see if the city already exists. cMain - holds the cWorld. It's the main window, has a toolbar and a display window. Anyway, I have the following code in cMain.java: Code:
I apologize if I'm unclear on any of this, please let me know if you need clarification on anything. Thanks so much. |
It sounds like you've got a solid design:
cMain: your "root" object; contains your "static void main()" and your UI +- cCity: another "root" object, presumably a singleton +- cWorld: the collection of cities I presume event handler "actionPerformed()" is registered with some or another pushbutton, that you've added to your UI, that you've created from cMain. So right off the bat (if I'm understanding you correctly), your program will indeed "stop" (more specifically, will "block" in the UI's "main event loop") until you hit some control. This is Good. I further presume that "actionPerformed()" will try to do an "addTo()" to "cWorld", and the implementation of method "cWorld.addTo()" will talk to some corresponding method in "cCity". And that "cCity.addTo()" will either accept or deny the request, and that "cWorld.addto()" will handle the success or failure accordingly. In either case, when "actionPerformed()" has completed your program should *not* (as I understand it) do anything but return back to the main event loop. You can *insure* this is the case by keeping your all of your objects (cCity, cWorld and the UI) under top-level class "cMain", and *not* inadvertantly putting them under the UI. I'm not sure if *I'm* making myself clear. But bottom line: I honestly think you're basically on the right track. At worst, I think you might be facing a minor "gotcha" that you can easily deal with by looking at the problem again (or, hopefully, reviewing my comments once again). 'Hope that helps .. PSM |
See, what's happening in this program is cCity temp = new cCity() creates a new city with uninitialized properties and opens up a new window ( cCityEditor class ), prompting the user to specify the city's properties. Unfortunately, after the window is created, the program goes on to add the city (with still uninitialized variables) to the cWorld ( myWorld, in this case). What I really want to do is, after creating the cCityEditor window, wait to advance until an action is performed. Unfortunately, I don't know how to do this....
|
Hi -
Like I implied, your basic question is a design question (not a "Java" question per se). And I think you've already got a basically sound design. Honest! I'd think about it a bit further, carefully considering: Roles ... and ... Responsibilities Which classes and which objects are responsible for what? When should they "do something", and when should they delegate to another class or class instance? Hint: think "MVC" (Model/View/Controller). Good luck .. PSM PS: Here's a good resource: http://www.mindview.net/Books/TIJ/ |
Oh wait. Nevermind, I actually created a workaround without realizing it. While the city is originally inserted into the world with uninitialized parameters, once the cCityEditor window closes, the properties are modified to the already inserted city. While it's not conducive to ultimate stability, it will definately work for now. Thanks for your help, I appreciate it.
|
You didn't show what the run() method does, but presumably it does a lot.
You just took over the windowing thread for your own purposes. Instead, make sure your cCity thing implements Runnable, and: Thread t = new Thread(c); t.start(); That will run your code in the background. And you might consider using normal Java conventions -- starting with lowercase letters in Javascript types is not great ;-) This has nothing to do with Linux, this is a pure java question. |
I disagree that logosys necessarily needs to spawn any more threads. All he needs to do is make sure that any "action" (be it a user action, or "something else") results in some discrete, finite response.
But if the response is indeed open-ended (as may or may not be implied by the method name "run()") well, then yes - spawning a thread is probably a good solution. But I'd only go there as a last resort, and I'd encourage him to explore any/all other options before going to threading... IMHO .. PSM |
Re: Java - Halting a Program until an action is performed.
[QUOTE]Originally posted by logosys
[ Code:
btnNewWorld.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ... } } ); |
Quote:
|
Quote:
|
On behalf of the members of LQ, I apologize if you feel condescended to. That certainly wasn't our intent. And I hope it doesn't discourage you from asking questions in the future.
Sincerely .. PSM |
>Forgive me if I misunderstood you
You did misunderstand. Adopting your own case convention is silly, and might someday get you into trouble when suddenly your name collides with a package, but I just made that comment in passing, that's your business. Your real stylistic problems are in writing lots of if statements to check which button is pressed. That kind of code is not good style, which tells me you are kind of new at it. Don't take it personally. I told you how to fix that, which will make your code cleaner, faster, and more maintainable. You build a listener per object, not one great big listener and write switch statements to determine which one was hit. It's not a huge jump from there to understanding that if you're doing that sort of thing, that the very reason you need to launch a thread may well be a design that is less than optimal. If you want to learn how to write code better, the first thing you need to do is get the chip off your shoulder. If you think your variable style is good, ignore the comment. I noticed you took a lot of umbrage at my comments (which most Java programmers would agree are dead on) but you ignored the fact that I solved your problem, as you asked. Only then did I reply to Paul's comment that, in fact, you probably were making a mistake in the first place. But that's a guess, because you didn't show that code. |
All times are GMT -5. The time now is 08:01 PM. |