LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2005, 01:16 PM   #1
logosys
Member
 
Registered: Jul 2005
Location: Texas
Distribution: RHEL4 - ES
Posts: 178

Rep: Reputation: 31
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.
 
Old 10-06-2005, 01:47 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 10-06-2005, 04:01 PM   #3
logosys
Member
 
Registered: Jul 2005
Location: Texas
Distribution: RHEL4 - ES
Posts: 178

Original Poster
Rep: Reputation: 31
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....
 
Old 10-06-2005, 04:05 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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/


Last edited by paulsm4; 10-06-2005 at 04:08 PM.
 
Old 10-06-2005, 04:07 PM   #5
logosys
Member
 
Registered: Jul 2005
Location: Texas
Distribution: RHEL4 - ES
Posts: 178

Original Poster
Rep: Reputation: 31
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.
 
Old 10-06-2005, 04:11 PM   #6
dovkruger
LQ Newbie
 
Registered: Nov 2003
Location: NY area
Distribution: redhat EE3, Fedore Core 4
Posts: 23

Rep: Reputation: 15
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.
 
Old 10-06-2005, 04:15 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 10-06-2005, 04:33 PM   #8
dovkruger
LQ Newbie
 
Registered: Nov 2003
Location: NY area
Distribution: redhat EE3, Fedore Core 4
Posts: 23

Rep: Reputation: 15
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) {
...
}
} );
 
Old 10-06-2005, 04:35 PM   #9
dovkruger
LQ Newbie
 
Registered: Nov 2003
Location: NY area
Distribution: redhat EE3, Fedore Core 4
Posts: 23

Rep: Reputation: 15
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.
 
Old 10-06-2005, 05:59 PM   #10
logosys
Member
 
Registered: Jul 2005
Location: Texas
Distribution: RHEL4 - ES
Posts: 178

Original Poster
Rep: Reputation: 31
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.
 
Old 10-06-2005, 08:01 PM   #11
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 10-07-2005, 03:17 PM   #12
dovkruger
LQ Newbie
 
Registered: Nov 2003
Location: NY area
Distribution: redhat EE3, Fedore Core 4
Posts: 23

Rep: Reputation: 15
>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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows performed better - what am I doing wrong?! endif Linux - Newbie 12 06-25-2008 12:05 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
Java action events AMMullan Programming 22 04-16-2004 04:57 PM
java jdk1.4.2 & free: installation performed but javac doesn't run Dominik *BSD 3 02-18-2004 03:42 AM
What is the most important task that should be performed on the Unix server? thatgentleman Linux - General 7 03-23-2002 09:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration