We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Applet without GUI

kretzschmar
kretzschmar asked
on
Medium Priority
668 Views
Last Modified: 2013-11-23
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 ;-)
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Just don't show the applet (dimensions of 0)
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sorry misinterpreted. Try

java -Djava.awt.headless=true
CERTIFIED EXPERT
Top Expert 2016

Commented:
(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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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() ;-)
Top Expert 2004

Author

Commented:
>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 ;-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
Perhaps you'd better post some code ...
Top Expert 2004

Author

Commented:
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 ;-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>You may need to call

(and implement)
Top Expert 2004

Author

Commented:
>as you are trying to use resources that are normally handled in an applet, GUI, context
which?

>(and implement)
how?
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>this.getDocumentBase()

for one. getParameter for the other. You have two alternatives:

a. bypass those calls and their ilk when using as an application
b. implement and set the AppletStub

>>how?

By instantiating a class that implements the methods of AppletStub

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2004

Author

Commented:
well, guess i go for a.

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

testing now

meikl ;-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
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.

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
just use the following:

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

Top Expert 2004

Author

Commented:
hi experts,

just a small status:

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

meikl ;-)
Top Expert 2004

Author

Commented:
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 ;-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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 :)
Top Expert 2004

Author

Commented:
>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 :-))
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you asked how to run your applet without a gui, the comment you accepted does *not* do that
Top Expert 2004

Author

Commented:
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 ;-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.