LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java - Halting a Program until an action is performed. (https://www.linuxquestions.org/questions/programming-9/java-halting-a-program-until-an-action-is-performed-370378/)

logosys 10-06-2005 01:16 PM

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:

   
public void actionPerformed( ActionEvent evt ) {
        if( evt.getSource() == btnNewWorld ){
                myWorld = new cWorld();
                } else if( evt.getSource() == btnNewCity ) {
                    InfraCity tempCity = new cCity();
                    myWorld.addCity( tempCity );
                } else if( evt.getSource() == btnRemoveCity ) {
                    InfraCity tempCity = new cCity();
                    myWorld.rmCity( tempCity );
                } else if( evt.getSource() == btnRun ) {
                    cWorld.run();
                }
      }
}

The problem is, the cCityEditor object is created, but then the program continues along it's merry way. I need the program to wait for the "Ok" button to be clicked. How can I get this to happen?

I apologize if I'm unclear on any of this, please let me know if you need clarification on anything. Thanks so much.

paulsm4 10-06-2005 01:47 PM

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

logosys 10-06-2005 04:01 PM

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....

paulsm4 10-06-2005 04:05 PM

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/


logosys 10-06-2005 04:07 PM

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.

dovkruger 10-06-2005 04:11 PM

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.

paulsm4 10-06-2005 04:15 PM

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

dovkruger 10-06-2005 04:33 PM

Re: Java - Halting a Program until an action is performed.
 
[QUOTE]Originally posted by logosys
[
Code:

   
public void actionPerformed( ActionEvent evt ) {
        if( evt.getSource() == btnNewWorld ){
                myWorld = new cWorld();
                } else if( evt.getSource() == btnNewCity ) {
                    InfraCity tempCity = new cCity();
                    myWorld.addCity( tempCity );
                } else if( evt.getSource() == btnRemoveCity ) {
                    InfraCity tempCity = new cCity();
                    myWorld.rmCity( tempCity );
                } else if( evt.getSource() == btnRun ) {
                    cWorld.run();
                }
      }
}

You should also lose the comparisons. This isn't good OO style. Instead, add a listener for each button:

btnNewWorld.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
}
} );

dovkruger 10-06-2005 04:35 PM

Quote:

Originally posted by paulsm4
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

I agree. Given all the other stylistic mistakes, the odds are there is something else wrong, and that the code does not have to be multithreaded.

logosys 10-06-2005 05:59 PM

Quote:

Originally posted by dovkruger
I agree. Given all the other stylistic mistakes, the odds are there is something else wrong, and that the code does not have to be multithreaded.
Forgive me if I misunderstood you, but I don't appreciate the condescension. So my variables don't follow the standard naming conventions. I don't use javadoc comments either, but you know what? I'm just trying to figure out how the dynamics of this language work. I don't think this program needs to be multithreaded, I just want the program to halt execution for a moment and wait for an action to be performed. I genuinely appreciate the help that people are trying to offer, and I would much appreciate constructive criticism, but if you're just going to tell me that, since I have my own naming convention for variables my entire program must be wrong is demeaning, narrow-minded and unwelcomed.

paulsm4 10-06-2005 08:01 PM

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

dovkruger 10-07-2005 03:17 PM

>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.