Passing arguments in Main - not simple

K

Kurt M Peters

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]);
 
R

Roedy Green

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

Stefan Ram

Roedy Green said:
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 ); }}); }}
 
P

Patricia Shanahan

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
 
K

Kurt M Peters

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

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top