Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Passing arguments in Main - not simple

Reply
Thread Tools

Passing arguments in Main - not simple

 
 
Kurt M Peters
Guest
Posts: n/a
 
      03-19-2006
Hello,
I understand how people normally would pass arguments to a program using
the command line using the Main method, but when using Swing people seem to
recommend starting the application in its own thread as shown below:

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new myJavaApplication().setVisible(true);
}
});
}

Normally, one could simply use args[] as an array, but since the
myJavaApplication is "encased" in an "invokeLater" internal method, I don't
see how to pass args to the application itself in any useful manner. Does
anyone have any suggestions for a way around this?
Regards,
Kurt

Normal way of using args (a possibility that's not possible using entrance
above):


if(null == args || args.length < 1) {
System.exit(1);
}
new myJaveApplication(args[0]);


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      03-19-2006
On Sun, 19 Mar 2006 22:34:24 GMT, "Kurt M Peters"
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>Normally, one could simply use args[] as an array, but since the
>myJavaApplication is "encased" in an "invokeLater" internal method, I don't
>see how to pass args to the application itself in any useful manner. Does
>anyone have any suggestions for a way around this?
>Regards,
>Kurt


here are three ways of doing it.

1. You could have the app itself implement Runnable then main can pass
it all the parms it wants to the constructor, or leave them lying
around in statics.

2. pass your Runnable some parameters in its constructor.

3. have your Runnable.run examine some local finals in main.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-19-2006
Roedy Green <(E-Mail Removed) > writes:
>2. pass your Runnable some parameters in its constructor.


In this case »args« might also need to be declared as »final«.

public class Main
{ public Main( final java.lang.Object object ){}
public void setVisible( final boolean value ){}
public static void main( final java.lang.String[] args )
{ java.awt.EventQueue.invokeLater(new Runnable()
{ public void run(){ new Main( args ).setVisible( true ); }}); }}

>3. have your Runnable.run examine some local finals in main.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-20-2006
Kurt M Peters wrote:
....
> Normally, one could simply use args[] as an array, but since the
> myJavaApplication is "encased" in an "invokeLater" internal method, I don't
> see how to pass args to the application itself in any useful manner. Does
> anyone have any suggestions for a way around this?

....

In addition to the answers already posted, remember that implementing a
Runnable as an anonymous inner class is an option, not a requirement.

Patricia
 
Reply With Quote
 
Kurt M Peters
Guest
Posts: n/a
 
      03-20-2006
Thanks for the quick response.

I hate to press my luck, but (1) seems "easiest" to me, although it
throws me clear out of my "safe zone". So, if I have it implement Runnable,
how would the inside look for the "run" method?

I'm assuming the main is changed to:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new myJavaApplication(args));
}

My problem is a lack of understand of threads. What would I need in "run"?
I assume it's called automatically?
Is this all I need?
public void run() {
setVisible(true);
}
thanks again,
Kurt

"Roedy Green" <(E-Mail Removed) > wrote in
message news:(E-Mail Removed)...
> On Sun, 19 Mar 2006 22:34:24 GMT, "Kurt M Peters"
> <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone who said :
>
>>Normally, one could simply use args[] as an array, but since the
>>myJavaApplication is "encased" in an "invokeLater" internal method, I
>>don't
>>see how to pass args to the application itself in any useful manner. Does
>>anyone have any suggestions for a way around this?
>>Regards,
>>Kurt

>
> here are three ways of doing it.
>
> 1. You could have the app itself implement Runnable then main can pass
> it all the parms it wants to the constructor, or leave them lying
> around in statics.
>
> 2. pass your Runnable some parameters in its constructor.
>
> 3. have your Runnable.run examine some local finals in main.
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-20-2006
On Mon, 20 Mar 2006 01:17:36 GMT, "Kurt M Peters"
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>My problem is a lack of understand of threads.

There are no threads created.. You are just borrowing the run method
of Runnable. All that happens is Swing will invoke your run method at
some point in future.

Your Runnable is very much like a Swing event handler. You can get
data in the same three ways.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-20-2006

On Mon, 20 Mar 2006 01:17:36 GMT, "Kurt M Peters"
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>I assume it's called automatically?
> Is this all I need?
> public void run() {
> setVisible(true);
> }
>thanks again


see http://mindprod.com/jgloss/japplet.html
for the complete code to turn an Applet into an application.

See http://mindprod.com/jgloss/jframe.html
for sample code to fire up an application with this technique.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
int main() or int main(void)? Frederick Ding C Programming 10 12-04-2005 12:44 AM
adding main.aspx.vb & main.aspx.resx under aspx John M ASP .Net 1 05-29-2005 09:27 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM



Advertisments