again applet with/without GUI

i asked this question for a week,
the probelm i had may had nothing to do with the gui
as the expert objects mentioned
>you asked how to run your applet without a gui, the comment you accepted does *not* do that

so to all experts, especially objects,
you are forced to provide a small sample
with following conditions

applet runs in a browser -> create a gui (maybe simple a button)
applet runs as application, with an parameter-option switch maybe gui=<0|1>
in case of 0, create no gui
in case of 1, create a gui

meikl ;-)
LVL 27
kretzschmarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
I told you at the time how to run it with/without a gui
0
objectsCommented:
so in effect you have 3 modes :)

1. applet with gui
2. application with gui
3. application without gui

For the 3rd case exactly how do you want it to work?
0
kretzschmarAuthor Commented:
hi CEHJ,

currently i have no problem, this question is just for interest
and for clarify my minds.

if you post a complete short sample with the conditions of the question,
then the points are yours.

meikl ;-)
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

kretzschmarAuthor Commented:
>so in effect you have 3 modes :)

yes,

>For the 3rd case exactly how do you want it to work?
without any useraction, maybe should run the code behind the button-event, a method-call of the applet

meikl ;-)

0
objectsCommented:
It will look something like this:

public class MyApp extends JApplet
{
   public void init()
   {
       add(new JButton("Test"));
   }

   public static void main(String[] args)
   {
       if (args.length>0 && args[0].equals("gui"))
       {
           // application with gui
           JFrame f = new JFrame();
           MyApp app = new MyApp();
           app.init();
           f.getContentPane().add(app);
           f.pack();
           f.setVisible(true);
       }
       else
       {
           // application without gui
           System.out.println("I don't have a gui");
       }
   }
}

0
CEHJCommented:
You can this as either

java AppletApplication

or

appletviewer AppletApplication.java



//===========================================


/*
<applet codebase="." code="AppletApplication.class" width="300" height="300">
</applet>
*/

import java.awt.EventQueue;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * 28-Mar-2006
 */

/**
 * @author Charles
 */
public class AppletApplication extends JApplet {
      
      public void startUp() {
            EventQueue.invokeLater(new Runnable() {
                  public void run() {
                        init();
                        JFrame f = new JFrame();
                        f.getContentPane().add(AppletApplication.this);
                        f.setSize(200, 200);
                        f.setVisible(true);
                  }
            });

      }

      /**
       * @param args The command line arguments when run as application
       */
      public static void main(String[] args) {
            new AppletApplication().startUp();
      }

      /* (non-Javadoc)
       * @see java.applet.Applet#init()
       */
      public void init() {
            super.init();
            getContentPane().add(new JButton("Click me!"));
      }

}
0
objectsCommented:
public class MyApp extends JApplet
{
   public void init()
   {
       add(new JButton("Test"));
   }

   public static void main(String[] args)
   {
       if (args.length>0 && args[0].equals("gui"))
       {
           // application with gui
           JFrame f = new JFrame();
           MyApp app = new MyApp();
           app.init();
           f.getContentPane().add(app);
           f.pack();
           f.setVisible(true);
       }
       else
       {
           // application without gui
           System.out.println("I don't have a gui");
           BufferReader in = new BufferedReader(new InputStreamReader(System.in));
           System.out.print("Please press enter?");
           in.readLine();
       }
   }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
> java AppletApplication

it needs to run as both gui and non-gui, that code only runs as a gui
0
CEHJCommented:
In that case, may the entire body of void startUp the following:

System.out.println("Hi, i'm not a GUI");
0
CEHJCommented:
may=make
0
CEHJCommented:
Corrected:

/*
 <applet codebase="." code="AppletApplication.class" width="300" height="300">
 </applet>
 */

import java.awt.EventQueue;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * 28-Mar-2006
 */

/**
 * @author Charles
 */
public class AppletApplication extends JApplet {

      public void startUp(String[] args) {
            if (args.length == 1 && "GUI".equalsIgnoreCase(args[0])) {
                  EventQueue.invokeLater(new Runnable() {
                        public void run() {
                              init();
                              JFrame f = new JFrame();
                              f.getContentPane().add(AppletApplication.this);
                              f.setSize(200, 200);
                              f.setVisible(true);
                        }
                  });
            } else {
                  System.out.println("Hi, i'm not a GUI");
            }

      }

      /**
       * @param args
       *          The command line arguments when run as application
       */
      public static void main(String[] args) {
            new AppletApplication().startUp(args);
      }

      /*
       * (non-Javadoc)
       *
       * @see java.applet.Applet#init()
       */
      public void init() {
            super.init();
            getContentPane().add(new JButton("Click me!"));
      }

}
0
objectsCommented:
now you just copying my suggestion ;)
0
CEHJCommented:
No,  just following the spec more closely
0
kretzschmarAuthor Commented:
well, ok,

thank you both

if i compare your suggestions,
then i would guess at objects sample,
that in case of application with no gui
the applet will not be created, right?

meikl ;-)
0
CEHJCommented:
>>the applet will not be created, right?

Correct
0
objectsCommented:
> that in case of application with no gui
> the applet will not be created, right?

Thats correct as its not needed.
Even for the application with gui it not used as an applet, just the panel is used.

0
kretzschmarAuthor Commented:
in your case CEHJ,

the constructor seems to be startUp(String[] args)
or is it just
new AppletApplication().startUp(args);
whereas
new AppletApplication() is the constructor
and startUp(args) is a method which is called immediatley
??

in case of objects sample
in case of application with no gui,
i will not be able to call a applet-method

is it the same bahaviour in your sample, CEHJ?

sorry i'm just new to java so i may reask simple facts

meikl ;-)
0
objectsCommented:
CEHJ has just rearranged things by moving the application initilisatioon out of main. Its really no different.
0
kretzschmarAuthor Commented:
so, in conclusion,
i have to avoid to create the applet-object in case of running it as application with no gui?
0
Mayank SAssociate Director - Product EngineeringCommented:
Well, you should create only the stuff that you need :) you can always include 1000 lines of code in a class and use only 10 out of it - but what's the use?
0
kretzschmarAuthor Commented:
>Well, you should create only the stuff that you need :)
your comment is not helpful and points not to the question, mayankeagle, just useless

lets say the applet has implemented a method, which can run
without a gui -> output to system.out
with a gui -> output into a textarea

hope this makes more sense for understanding my intention for this question

meikl ;-)

0
objectsCommented:
> i have to avoid to create the applet-object in case of running it as application with no gui?

you don't *have* to, but the Applet class is a GUI class so not needed.
0
Mayank SAssociate Director - Product EngineeringCommented:
>> your comment is not helpful and points not to the question, mayankeagle, just useless

I will make sure I add you to my black-list hereafter and don't post on any of your questions. There is no need of writing code like:

if ( .... "gui" )
{
  show the applet () ;

}
else
  dont show the applet () ;

Rather than understanding how certain things can be done, you need to understand *why* they should be done. An applet is meant for solving a different set of problems.

http://java.sun.com/applets/

*Unsubscribed from this question*.
0
kretzschmarAuthor Commented:
>I will make sure I add you to my black-list hereafter
i have no problem to be on a blacklist (if someone need this, ok) :-))

>you need to understand *why* they should be done
thats why i ask

meikl  ;-)
0
kretzschmarAuthor Commented:
well, back to the Experts, who are not insulted by my direct kind.
btw. i have forgot to say bye to mayankeagle

>An applet is meant for solving a different set of problems.
so how would an expert solve a task to have a whatever java-object,
which should run in a browser and as application with the described conditions

browser = gui
application without/with gui

whereas process-steps with gui are sent to a textarea,
without gui process-steps are sent to the system-console

i raised the points,
as the question is now a bit expanded

meikl ;-)
0
objectsCommented:
code posted will already basically do that
0
kretzschmarAuthor Commented:
>code posted will already basically do that
well, ok,
but as stated in the conclusion above, that the applet is not created, in case of  no gui,
then the process-method cannot be an method of the applet, or am i wrong, in this case?

meikl ;-)
0
CEHJCommented:
The main(String[] args) method would only be used in the case of an application. That's the first (entry point) point of difference
0
kretzschmarAuthor Commented:
well, sure, that i can decide, if the main is called, the code runs as application

back to the process-method, which should be accessable on all kinds of possibility startups

should it be declared like the main?

public static void doSomething() {...}

?

meikl ;-)
0
CEHJCommented:
No. It should be an instance method or it would not be called by an applet context
0
objectsCommented:
the examples posted don't have any process code, thaqt would need to be added, prefereably in an entirely seperate class that would be used by both applet and application.
0
CEHJCommented:
>>the examples posted don't have any process code

The process code is in init in my example, called by applet and application alike
0
kretzschmarAuthor Commented:
well, now i'm a bit confused :-))

to CEHJ,
in case of an instance-method,
if it runs as application with no gui,
there is no instance created, or?

to objects,
in case of a seperate class a reference may needed,
to the textarea-object, if with gui, or?
 
meikl ;-)
0
CEHJCommented:
>>
if it runs as application with no gui,
there is no instance created, or?
>>

Yes, one is created in my example. One should be - Java is an object-oriented language
0
objectsCommented:
if no gui, then stdout, System.out
0
kretzschmarAuthor Commented:
well, ok,
i think i got it now

i will split the points equal between you both, objects and CEHJ

thanks for your time, your patient with me and your help

meikl ;-)
0
CEHJCommented:
:-)
0
kretzschmarAuthor Commented:
my english is not good :-(

thanks for your time, your patience with me and your assistance

:-))
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.