Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2005

Tip: Looking for answers? Try searching our database.

JFrame as JApplet?

Thread view: 
polsa - 27 Jun 2005 11:56 GMT
Hi all,
I was wondering if there is any way to write an application which can
be run as well as a JFrame as as a JApplet. And if so: how can I figure
out which way the application has been started?

Thanks in advance, Filip
Andrew Thompson - 27 Jun 2005 12:21 GMT
> I was wondering if there is any way to write an application which can
> be run as well as a JFrame as as a JApplet.

Yes.  It is fairly basic stuff [1].

> ..And if so: how can I figure
> out which way the application has been started?

- Tell your class ..
 - in the constructor or
 - through a method,
- hand it the parent UI and have it deternime,
- any number of other ways,
..depending upon you design, constraints and code.

[1] trivial example..
<sscce>
import javax.swing.*;

public class AppletApplicationGUI extends JApplet {

 JLabel message;

 public void init() {
   getContentPane().add( getGUIPanel() );
   setMessage("Applet Mode");
 }

 public JPanel getGUIPanel() {
   message = new JLabel("          ");
   JPanel p = new JPanel();
   p.add(message);
   return p;
 }

 public void setMessage(String text) {
   message.setText(text);
 }

 public static void main(String[] args) {
   JFrame f = new JFrame("Applet Application GUI");
   AppletApplicationGUI aag = new AppletApplicationGUI();
   f.getContentPane().add( aag.getGUIPanel() );
   aag.setMessage("Application Mode");
   f.pack();
   f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   f.setVisible(true);
 }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Thomas Weidenfeller - 27 Jun 2005 12:38 GMT
> I was wondering if there is any way to write an application which can
> be run as well as a JFrame as as a JApplet. And if so: how can I figure
> out which way the application has been started?

If main() is called, then it was started as an application. I haven't
done this in ages, but the following should start the applet when it is
called as an application:

    public class MyAppletStub implements AppletStub {
        // implement all AppletStub methods here
       
        public MyAppletStub(String args[]) {
            // parse and remember command line
        }
    }

    public static void main(String args[]) {
        JApplet a = new MyJApplet();
        a.setStub(new MyAppletStub(args));
        a.init();
           a.start();
    }

There are other things which might need to be adjusted. My notes tell me
that I got the above from some Sun article, but I can't find the
original article at the moment.

Maybe you probably better use Sun's AppletViewer to run the applet as an
application.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Thomas Fritsch - 27 Jun 2005 14:12 GMT
>> I was wondering if there is any way to write an application which can
>> be run as well as a JFrame as as a JApplet. And if so: how can I figure
[quoted text clipped - 22 lines]
> that I got the above from some Sun article, but I can't find the
> original article at the moment.
Seems that you refer to this article:
http://java.sun.com/developer/technicalArticles/Programming/TurningAnApplet/

> Maybe you probably better use Sun's AppletViewer to run the applet as an
> application.
>
> /Thomas

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Thomas Weidenfeller - 27 Jun 2005 14:26 GMT
> Seems that you refer to this article:
> http://java.sun.com/developer/technicalArticles/Programming/TurningAnApplet/ 

I really don't remember :-) But that one looks indeed like a good start.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Andrew Thompson - 27 Jun 2005 15:22 GMT
>> Seems that you refer to this article:
>> http://java.sun.com/developer/technicalArticles/Programming/TurningAnApplet/ 
>
> I really don't remember :-) But that one looks indeed like a good start.

For hosting an existing applet (with no source) in an application,
I agree, but the OP has the means to go a much easier route.  

Since they control the code, they can implement it this GUI as a
Container/(J)Panel which can then be used in whatever it is needed.

Note that article ends with..
"The AppletStub technique is easier to do than rearchitecting, "

*

"...but may still not work completely if your applet depends on the
AppletContext for anything. "

Implementing an applet context that is even half way reasonable
is no easy matter.  I wrote one that went slightly further than
the applet context provided for the applet viewer (mine hooked in
to BrowserLauncher to implement showDocument(URL)), but that took
some time to get right, and did not go to the more complex
abilities such as getting an enumeration of all applets (only one
could be opened at a time anyway), getting the stream keys, etc..

When you consider how tricky it is to reimplement some of
the aspects of the applet context that are commonly used by applets,
it becomes clear that it is easier to do the GUI as a Container
and use it as and when necessary, whether that usage is in a
(J)Applet, (J)Frame, (J)Window, (J)Dialog, another Container or
(J)Panel *or* JOptionPane.

* And I disagree with '..easier to do than rearchitecting,'
(unless you lack the source to the applet) because most applets
_do_ depend on the applet context.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.