Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Passing arguments in Main - not simple

Thread view: 
Kurt M Peters - 19 Mar 2006 23:34 GMT
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]);
Roedy Green - 19 Mar 2006 23:45 GMT
On Sun, 19 Mar 2006 22:34:24 GMT, "Kurt M Peters"
<nospampeterskurt@atmsn.com> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Stefan Ram - 20 Mar 2006 00:00 GMT
>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.
Kurt M Peters - 20 Mar 2006 02:17 GMT
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

> On Sun, 19 Mar 2006 22:34:24 GMT, "Kurt M Peters"
> <nospampeterskurt@atmsn.com> wrote, quoted or indirectly quoted
[quoted text clipped - 17 lines]
>
> 3. have your Runnable.run examine some local finals in main.
Roedy Green - 20 Mar 2006 02:24 GMT
On Mon, 20 Mar 2006 01:17:36 GMT, "Kurt M Peters"
<nospampeterskurt@atmsn.com> 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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 20 Mar 2006 02:25 GMT
On Mon, 20 Mar 2006 01:17:36 GMT, "Kurt M Peters"
<nospampeterskurt@atmsn.com> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Patricia Shanahan - 20 Mar 2006 01:24 GMT
...
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.