[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

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 ;-)
0
kretzschmar
Asked:
kretzschmar
  • 13
  • 12
  • 11
  • +1
2 Solutions
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 13
  • 12
  • 11
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now