Can I combine GUI & Applet?

M

Martin Gregorie

I know how to write a program that can be used as a command line utility
or with a standard Swing GUI.

Is it possible to extend the same program so it can also run as an applet?

I suspect the answer is NO, at least not without duplicating a lot of
presentation code because JApplet and JFrame look to be mutually
exclusive. If I've missed something and it is possible I'd appreciate a
pointer or two as to where to look to find out how to do it.

TIA
Martin
 
A

Andrew Thompson

Martin said:
I know how to write a program that can be used as a command line utility
or with a standard Swing GUI.

Is it possible to extend the same program so it can also run as an applet?

It is trivially easy to convert a Swing layout to
a JApplet (just add the components to the JApplet,
and you're 'done', security notwithstanding. However..
I suspect the answer is NO, at least not without duplicating a lot of
presentation code because JApplet ...

...applets are not for the inexperienced, and will introduce
a slew of exciting new problems you never dreamed of.

Why would your users prefer an applet to a web-start based
application? (E.G. A web-based launch of the JFrame).

What is the advantage of the applet (as you see it)?

Andrew T.
 
M

Martin Gregorie

Andrew said:
It is trivially easy to convert a Swing layout to
a JApplet (just add the components to the JApplet,
and you're 'done', security notwithstanding. However..
Yes, I did that to produce an applet from the command line utility/GUI
application, pausing only to strip out a lot of no-longer needed stuff
such as main() and all the menu-bar related code.
..applets are not for the inexperienced, and will introduce
a slew of exciting new problems you never dreamed of.
I don't need to guess about that!
Why would your users prefer an applet to a web-start based
application? (E.G. A web-based launch of the JFrame).
The applet does what I and my users want in this case.

However, there are a few cases, mostly small applications, where it
would be nice to be able to execute the same code as a command line
utility, a stand-alone GUI application or embed it in a web page. This
would let me run it as command line or GUI but be able to share it more
widely by publishing it on a web page, and sounds as if it should be
less work that building three separate applications with a lot of common
classes.
What is the advantage of the applet (as you see it)?
No distribution effort needed and the sandbox to reassure those of a
nervous disposition.

I know I should also look at WebStart, though I'm not all that keen on
using something that can do major installs on other people's hardware.
 
A

Andrew Thompson

Martin Gregorie wrote:
.....
The applet does what I and my users want in this case.

Scenario "web page with applet opens, URL parameters are
written into the page to indicate the applet should just
'start processing'".

Sound simple enough, huh? It ain't rocket science..

Applet Gotcha's
34/137 - This will not work in IE 7, as users need to specifically
'click on' an applet before it becomes active. Unless you
write the script element using JS (which will fail for any
browser that has JS disable)
However, there are a few cases, mostly small applications, where it
would be nice to be able to execute the same code as a command line
utility, a stand-alone GUI application or embed it in a web page.

Why the last one? Why not a button/link in a web page
that launches the application? (Which is how JWS apps.
are launched).
...This
would let me run it as command line or GUI but be able to share it more
widely by publishing it on a web page, and sounds as if it should be
less work that building three separate applications with a lot of common
classes.

Think again. It's a lot quicker to wrap an application
in a JLNP file (and sign the jars if necessary), than to
get an applet written, tested and deployed.
No distribution effort needed and the sandbox to reassure those of a
nervous disposition.

Web-start has both those features.
Try this link and 'cancel' it if you see anything
that would panic your users.
I know I should also look at WebStart, though I'm not all that keen on
using something that can do major installs on other people's hardware.

A 'full permissions' web-start app. is little more
'bells and whistles' than the equivalent applet, and
a sandboxed install (like above) is no drama.

Further, is the user is prompted to 'update Java'
(which could be construed as 'bells and whistles'),
then it can be concluded that the evquivalent applet
would simply have failed in its basic form.

Andrew T.
 
M

Martin Gregorie

Andrew said:
Applet Gotcha's
34/137 - This will not work in IE 7, as users need to specifically
'click on' an applet before it becomes active. Unless you
write the script element using JS (which will fail for any
browser that has JS disable)
Opera 9 also does this, with an initial pop-up window saying

"Title: click to activate and use this control"
Web-start has both those features.
Try this link and 'cancel' it if you see anything
that would panic your users.
<http://www.physci.org/pc/jtest.jnlp>
For some reason (probably an Opera 9 quirk) that didn't run until I cut
'n pasted it into the URL box.

Then it went through Opera's Transfers screen & dialog into a
"Downloading Java" screen. Was that expected?
Further, is the user is prompted to 'update Java'
(which could be construed as 'bells and whistles'),
then it can be concluded that the evquivalent applet
would simply have failed in its basic form.
Fair comment.

Thanks for your help, which has answered my question. I now know that a
given chunk of code can't be both applet and GUI application.
 
A

Andrew Thompson

Martin Gregorie wrote:
....
Then it went through Opera's Transfers screen & dialog into a
"Downloading Java" screen. Was that expected?

Sh*te! I have been promising myself for some time,
that I am going to develop a 'rock solid'* launch for
web-start projects, that either manages to launch
the project smoothly, or keeps the user well informed
about how to correct problems/why it failed.

* Or as 'rock solid' as one might expect in this
internet in which we have browser plug-ins that
will remove elements (such as scripts and applet
elements, amongst other things) without any
overt warning or message to the end-user.

As you may have noticed, I had not gotten around
to writing that page yet, and was instead relying on
developers having 'web-start friendly' browsers.

Stupid assumption on my part.

So the shorter answer is, your result (especially for
Opera) was "Kind of expected, but not optimal".

(Grumbles.. Bloody 'Opera')
Thanks for your help, which has answered my question. I now know that a
given chunk of code can't be both applet and GUI application.

Huhh?! I have failed somewhere in this thread
(yes, besides the failed launch).

You can
- throw a main() into a regular applet that shows it in
a frame,
- develop a GUI in a JPanel that can be dropped
into a JFrame or a JApplet, or the equivalent
- Panel that can be put in a Frame or Applet..

I am working on a project at the moment that has both a
Frame and an Applet, and they are both 'thin wrappers'
around the guts of the program, which is programmed in
a Panel.

Huge chucks of (GUI) code can most certainly be
reused if developing both an application and applet.

Andrew T.
 
M

Martin Gregorie

Andrew said:
Martin Gregorie wrote:
...

Sh*te! I have been promising myself for some time,
that I am going to develop a 'rock solid'* launch for
web-start projects, that either manages to launch
the project smoothly, or keeps the user well informed
about how to correct problems/why it failed.
Well, it is rock solid in as much that I didn't have to prod anything
except to tell Opera whether the save or open the download. This is
normal: I hadn't downloaded anything with that extension before. Once
I'd clicked "Open" it all went straight through and popped up your
window with a correct list of the stuff I was running.

So the shorter answer is, your result (especially for
Opera) was "Kind of expected, but not optimal".
OK

(Grumbles.. Bloody 'Opera')
Yes, I have the impression that it may have gone downhill a bit since
version 7 - creeping featuritis.

It also has a few annoyances, like getting stuck in some websites unless
I use the history list to skip a level or two, though I'm not sure it
that's really opera or more properly down to web sites with mickey mouse
"intelligent" invisible initial pages.
Huhh?! I have failed somewhere in this thread
(yes, besides the failed launch).
As you didn't answer the question directly I assumed, evidently wrongly,
that it wasn't possible.
You can
- throw a main() into a regular applet that shows it in
a frame,
- develop a GUI in a JPanel that can be dropped
into a JFrame or a JApplet, or the equivalent
- Panel that can be put in a Frame or Applet..
That's clear and makes sense. Thanks.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top