Passing arguments in Main - not simple

Discussion in 'Java' started by Kurt M Peters, Mar 19, 2006.

  1. 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]);
    Kurt M Peters, Mar 19, 2006
    #1
    1. Advertising

  2. Kurt M Peters

    Roedy Green Guest

    On Sun, 19 Mar 2006 22:34:24 GMT, "Kurt M Peters"
    <> 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.
    Roedy Green, Mar 19, 2006
    #2
    1. Advertising

  3. Kurt M Peters

    Stefan Ram Guest

    Roedy Green <> 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.
    Stefan Ram, Mar 19, 2006
    #3
  4. 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
    Patricia Shanahan, Mar 20, 2006
    #4
  5. 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" <> wrote in
    message news:...
    > On Sun, 19 Mar 2006 22:34:24 GMT, "Kurt M Peters"
    > <> 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.
    Kurt M Peters, Mar 20, 2006
    #5
  6. Kurt M Peters

    Roedy Green Guest

    On Mon, 20 Mar 2006 01:17:36 GMT, "Kurt M Peters"
    <> 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.
    Roedy Green, Mar 20, 2006
    #6
  7. Kurt M Peters

    Roedy Green Guest

    On Mon, 20 Mar 2006 01:17:36 GMT, "Kurt M Peters"
    <> 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.
    Roedy Green, Mar 20, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M
    Replies:
    1
    Views:
    1,118
    Kumar Reddi
    May 29, 2005
  2. Hal Styli
    Replies:
    14
    Views:
    1,617
    Old Wolf
    Jan 20, 2004
  3. Frederick Ding

    int main() or int main(void)?

    Frederick Ding, Dec 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    642
  4. Ravi
    Replies:
    17
    Views:
    914
    Kenneth Brody
    Apr 1, 2006
  5. oldyork90
    Replies:
    10
    Views:
    337
    Jorge
    Sep 27, 2008
Loading...

Share This Page