Link to home
Start Free TrialLog in
Avatar of kretzschmar
kretzschmarFlag for Germany

asked on

Applet without GUI

Hello experts,

i have a task to code an applet,
which shall run in a browser and as an java-application

in case of java-application it may run on machines without a configured DISPLAY -> no GUI

is it possible to disable the GUI in case of such configured machines?

meikl ;-)
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Just don't show the applet (dimensions of 0)
Sorry misinterpreted. Try

java -Djava.awt.headless=true
(where you're running as an application on a display-free box)
The problem is, if you intend to use your applet in a browser, then you won't be able to use it as an application because there is no main().

However, if you're talking about having the same application with GUI mode and Console mode, then yes definately can be done.
>>then you won't be able to use it as an application because there is no main().

I'm assuming kretzschmar knows this and has made a main() ;-)
Avatar of kretzschmar

ASKER

>I'm assuming kretzschmar knows this and has made a main() ;-)
yes, i have a main()

>(where you're running as an application on a display-free box)
i tried it on a linux-box, got an error there because of no display,
the applet aborts in this case

a collegue configured then an xwindows and it works fine then,
but it should also work without any gui

>However, if you're talking about having the same application
>with GUI mode and Console mode, then yes definately can be done.

how?

usual i guess i got this error during some kind of awt-initialization
i get this error also, if i do not create any control

any suggestion are welcome

meikl ;-)
Perhaps you'd better post some code ...
well okay,

my main

  public static void main(String[] args) {
      TransferApplet applet = new TransferApplet();
    applet.init();
    if (applet.useGUI) {
      Frame frame = new Frame();
      frame.setTitle("TransferApplet");
      frame.add(applet);
      frame.setSize(400, 420);
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
      frame.setVisible(true);
      frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
    }
    applet.start();
  }

my init()

  public void init() {
    try  {
      try {
        ConfigFile = this.getDocumentBase().getRef();
      }
      catch (Exception e) {
        ConfigFile = null;
      }
      if (ConfigFile == null) {
        ConfigFile = getParameter("ConfigFile",null);
      }
      if (ConfigFile == null) {
         throw new Exception("Konfigurationsdatei nicht angegeben!");
      }
      props.load(new FileInputStream(ConfigFile));
      if (props.getProperty("GUI").compareTo("1") == 0) {
        useGUI = true;  //this global var is defaulted to false
        ctlInit();  
      }
    }
    catch (Exception e) {
      handleException(e);
    }

so nothing special, i guess (i'm just a beginner :-))

meikl ;-)
You may need to call http://java.sun.com/j2se/1.5.0/docs/api/java/applet/Applet.html#setStub(java.applet.AppletStub)

as you are trying to use resources that are normally handled in an applet, GUI, context
>>You may need to call

(and implement)
>as you are trying to use resources that are normally handled in an applet, GUI, context
which?

>(and implement)
how?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well, guess i go for a.

just wondering what this will have to do with the DISPLAY error i got

testing now

meikl ;-)
I assume of course, you have passed the correct command line parameter per my original comment
What I ment initially when I said a GUI mode and console mode was basically 2 classes. One which extends JApplet, another which is just a console mode application with your regular BufferedReader.

So when you start your program you have to specify

java myProgram -mode 1

something like that.

just use the following:

  public static void main(String[] args) {
    TransferApplet applet = new TransferApplet();
    applet.init();
    applet.start();
  }

hi experts,

just a small status:

the linuxbox is currently not available,
but will get it today -> be patient

meikl ;-)
ok, get it work now

usual is was only this line

this.getDocumentBase();

i chanced the init and main as follows

(init)
...
      if (!isApplication) {
        try {
          ConfigFile = this.getDocumentBase().getRef();
        }
        catch (Exception e) {
          ConfigFile = null;
        }
      }
...

(main)
    ...
    applet.isApplication = true;  //isApplication is defaulted to false
    applet.init();
    ...

so points gets CEHJ

thanks for your help and time

meikl ;-)
:-)
thats still got a gui, if you just want to run it as an app or an  applet you can acheive a lot better than that :)
>thats still got a gui, if you just want to run it as an app or an  applet you can acheive a lot better than that :)

explain more, i'm still learning :-))
you asked how to run your applet without a gui, the comment you accepted does *not* do that
usual the graded solved my problem i had

but anyway i will provide some points,
if you give me 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 ;-)